Get gmail attachment's downloading URL? - ruby

I am fetching Gmail Attachment through Gmail API using the get_user_message_attachment method , but the data it sends is in Base64 encoded form. I want to get the attachment's downloadable URL, which I can store and send to the frontend of my application. Is it possible to get the URL of the attachment in some way? I do not want to convert the Base64 string to a file on my server and then upload it somewhere and send that upload's link to the front-end.
#gmail_service = Google::Apis::GmailV1::GmailService.new
#authorization stuff is done and then I fetch the attachment which is received as a Google::Apis::GmailV1::MessagePartBody response
resp = #gmail_service.get_user_message_attachment("myemail#google.com", message_id, attachment_id)
# resp.data contains the base64 encoded string
I want the attachment as a downloadable URL, so that I don't have to do the manual file conversion and uploading stuff.

The message.get method returns a Message object
This object contains the body of the message base64 encoded.
If you want a downloadable url I suggest that you convert the Base64 string to a file on your server and then upload it somewhere and send that upload's link to the front-end.
There is no other option this is the data the API returns.

You may want to consider providing a proxy endpoint on your server for your frontend. Then give your frontend a link to this endpoint with a messageId, and make your server download the Gmail message and stream it to your client.
This is Aqueduct(Dart) sample code:
Have a dedicated route for attachments (tempToken&fileName is optional, also contentType can be derived from attachment data):
..route('/attachments/:tempToken/:msgId/:attachmentId/:fileName').link(()
=> AttachmentsController(ctx))
The controller essentially loads an attachment by msgId&attachmentId and gives its content to a client:
#Operation.get('tempToken', 'msgId', 'attachmentId', 'fileName') Future<Response> getAttachment(
#Bind.path('tempToken') String tempToken, //
#Bind.path('msgId') int msgId,
#Bind.path('attachmentId') String attachmentId,
#Bind.query('contentType') String contentType) async {
...
final attachment = await mailbox.readAttachment(messageId,attachmentId);
return Response.ok(attachment)
..encodeBody = false
..contentType = ContentType.parse(contentType); }
We use Google provided Gmail API library. This is mailbox.readAttachment:
#override
Future<List<int>> readAttachment(String messageId, String attachmentId) async => (await _gmail.users.messages.attachments.get('me', messageId, attachmentId)).dataAsBytes;

Related

HTML email is getting displayed as plain/text with javamail in Gmail and Yahoo

Trying to send HTML email with java-mail . I can see HTML format email in Outlook but Gmail and Yahoo don't show HTML format , they are showing email as plain texts without HTML formatting .
I am using company's SMTP server to send an email to users .
I tried following and msgcontent is StringBuilder in following code :
Properties prop = System.getProperties();
prop.put("mail.smtp.auth", "false");
prop.put("mail.smtp.starttls.enable","false");
prop.put("mail.smtp.host", SMTP_SERVER);
Session session = Session.getInstance(prop);
MimeMessage msg = new MimeMessage(session);
MimeMultipart multipart = new MimeMultipart();
MimeBodyPart content = new MimeBodyPart();
content.setHeader("content-type", "text/html");
msg.setFrom(new InternetAddress(EMAIL_FROM));
msg.setRecipients(Message.RecipientType.TO, InternetAddress.parse(EMAIL_TO, false));
msg.setSubject(EMAIL_SUBJECT);
content.setContent(msgcontent.toString(), "text/html");
multipart.addBodyPart(content);
msg.setContent(multipart);
I want HTML email to be displayed in all email clients like Gmail , Yahoo, etc. Currently only Outlook can display HTML content.
You don't really need a multipart since your message has only one part.
You can simplify the content setting by replacing the two content.set* lines with:
content.setText(msgcontent.toString(), "utf-8", "html");
What version of JavaMail are you using?
If you examine the raw MIME content of the message you receive and compare it with the raw MIME content of the message you send, are they different?

POST a single large file in .net core

I have a .net core 2.1 api application that will download a file from a remote location based on the file name. Here is the code:
static public class FileDownloadAsync
{
static public async Task DownloadFile(string filename)
{
//File name is 1GB.zip for testing
Stopwatch stopwatch = new Stopwatch();
stopwatch.Start();
using (HttpClient client = new HttpClient())
{
string url = #"http://speedtest.tele2.net/" + filename;
using (HttpResponseMessage response = await client.GetAsync(url, HttpCompletionOption.ResponseHeadersRead))
using (Stream readFrom = await response.Content.ReadAsStreamAsync())
{
string tempFile = $"D:\\Test\\{filename}";
using (Stream writeTo = File.Open(tempFile, FileMode.Create))
{
await readFrom.CopyToAsync(writeTo);
}
}
stopwatch.Stop();
Debug.Print(stopwatch.Elapsed.ToString());
}
}
}
This is working great, it will pull a 1 gig file down in about 50 seconds. Well within the required download time. I have hard coded a test file to download in this code for testing as well as storage location--these values will ultimately come from a config file when moved into production. Here is the API endpoint that calls this function:
[HttpGet("{fileName}")]
public async Task<string> GetFile(string fileName)
{
await FileDownloadAsync.DownloadFile(fileName);
return "Done";
}
So getting the file from a remote location down to the local server is not a problem. I need some help/guidance on re-posting this file to another API. Once the file is downloaded, there is some work done on the file to prepare it for upload (the files are all MP4 files), and once that work is done, I need to post it to another API for more proprietary processing. Here is the API end point data I have:
POST: /batch/requests Allocates resources to start new batch transcription. Use this method to request[work] on the input
audio data. Upon the accepted request, the response provides
information about the associated request ID and processing status.
Headers: Authorization: Authorization token
Accept: application/json
Content-Type: Indicates the audio format. The value must be:
audio/x-wav;codec=pcm;bit=16;rate=8000;channels=1
audio/x-wav;codec=pcm;bit=16;rate=16000;channels=1
audio/x-raw;codec=pcm;bit=16;rate=8000;channels=1
audio/x-raw;codec=pcm;bit=16;rate=16000;channels=1
video/mp4
Content-Length (optional): The size of the input voice file. Not
required if a chunked transfer is used.
Query string parameters (required):
profileId: one of supported (see GET profiles) customerId: the id of
the customer. A string of minimum 1 and up to 250 alphanumeric, dot
(.) and dash (-) characters.
So I will set the Content-Type to video/MP4 for processing. Note that if the input size is not used if a chunked transfer is used.
Right now, I am more concerned with just posting (streaming) the file in a non-chunked format while we await for more information on what they consider "chunking" a file.
So I am looking for help on steaming the file from disk to the endpoint. Everything I am running across for .net core API is creating the API to download the file from a POST like a Razor page or Angular page--I already have that. I just need some help on "re-posting" to another API.
Thanks
Using the HttpClient you open a stream to the file, create a content stream, set the necessary headers and post to the endpoint
Stream file = File.Open(filepath, FileMode.Open);
var content = new StreamContent(file);
content.Headers.ContentType = new MediaTypeHeaderValue("video/MP4");
client.DefaultRequestHeaders.Add("Authorization", "token here");
client.DefaultRequestHeaders.Accept.Clear();
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json");
using (HttpResponseMessage response = await client.PostAsync(url, content)) {
//...
}

Login to web server with Google Sign-in

I'm connecting to a 3rd party web server from an HTTP client (Java or Dart - Android app) to download some resources (XML or IMG files) that belong to the current user on that server. This site requires login with Google Sing-In. I have everything set up in my Android app to login the user with Google, I obtained their authorization idToken. But how do actually use it in HTTP GET or POST methods to download the protected resources?
With BASIC authentication it's easy - just set HTTP 'Authorization' header correctly ("Basic " + user:password encoded as base64), call GET, and I download the desired resource. But I cannot find any information on how to do this with Google Sing-In. Do I send the idToken I received from Google in some headers? What other magic is needed?
Adding a Java code snippet, hope it helps:
// (Receive authCode via HTTPS POST)
if (request.getHeader('X-Requested-With') == null) {
// Without the `X-Requested-With` header, this request could be forged. Aborts.
}
// Set path to the Web application client_secret_*.json file you downloaded from the
// Google API Console: https://console.developers.google.com/apis/credentials
// You can also find your Web application client ID and client secret from the
// console and specify them directly when you create the GoogleAuthorizationCodeTokenRequest
// object.
String CLIENT_SECRET_FILE = "/path/to/client_secret.json";
// Exchange auth code for access token
GoogleClientSecrets clientSecrets =
GoogleClientSecrets.load(
JacksonFactory.getDefaultInstance(), new FileReader(CLIENT_SECRET_FILE));
GoogleTokenResponse tokenResponse =
new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
clientSecrets.getDetails().getClientId(),
clientSecrets.getDetails().getClientSecret(),
authCode,
REDIRECT_URI) // Specify the same redirect URI that you use with your web
// app. If you don't have a web version of your app, you can
// specify an empty string.
.execute();
String accessToken = tokenResponse.getAccessToken();
// Use access token to call API
GoogleCredential credential = new GoogleCredential().setAccessToken(accessToken);
Drive drive =
new Drive.Builder(new NetHttpTransport(), JacksonFactory.getDefaultInstance(), credential)
.setApplicationName("Auth Code Exchange Demo")
.build();
File file = drive.files().get("appfolder").execute();
// Get profile info from ID token
GoogleIdToken idToken = tokenResponse.parseIdToken();
GoogleIdToken.Payload payload = idToken.getPayload();
String userId = payload.getSubject(); // Use this value as a key to identify a user.
String email = payload.getEmail();
boolean emailVerified = Boolean.valueOf(payload.getEmailVerified());
String name = (String) payload.get("name");
String pictureUrl = (String) payload.get("picture");
String locale = (String) payload.get("locale");
String familyName = (String) payload.get("family_name");
String givenName = (String) payload.get("given_name");
For detailed info, find all the required steps and references at: https://developers.google.com/identity/sign-in/web/server-side-flow#step_1_create_a_client_id_and_client_secret

Custom Bot always replies with an error

I am trying to send a webhook out from Teams, which is apparently accomplished with a Custom Bot. I am able to get the bot created and then I can do #botname stuff and the endpoint receives a payload.
However, the bot immediately replies with "Sorry, there was a problem encountered with your request". I get this error if I point the "Callback URL" to a requestb.in url or if I point it to my endpoint. This leads me to suspect the bot is expecting some specific response from the endpoint, but this isn't documented. My endpoint responds with a 202 and some json. Requestb.in responds with a 200 and "ok".
So, is it true that the bot requires a specific response payload and if so what is this payload?
That link above mentions Your custom bot will need to reply asynchronously to the HTTP request from Microsoft Teams. It will have 5 seconds to reply to the message before the connection is terminated. But there is no indication of how to satisfy this request, unless the custom bot will need to reply synchronously.
You need to return a JSON response with the keys 'text' and 'type' as shown in the example here
{
"type": "message",
"text": "This is a reply!"
}
In case you are using NodeJS, you can try this sample code
I created an azure function in C# as the callback for the custom bot and was initially sending back a json string but that didnt work. Finally I had to set the response object's Content and ContentType to get it working (as shown here). Here is the code for a simple bot that echoes back what the user types in the channel, feel free to adapt it to your scenario.
Custom MS Teams bot example code using azure functions
#r "Newtonsoft.Json"
using System.Net;
using System.Net.Http.Headers;
using Newtonsoft.Json;
public static async Task<HttpResponseMessage> Run(HttpRequestMessage req, TraceWriter log)
{
log.Info("C# HTTP trigger function processed a request.");
// parse query parameter
string name = req.GetQueryNameValuePairs()
.FirstOrDefault(q => string.Compare(q.Key, "name", true) == 0)
.Value;
// Get request body
dynamic data = await req.Content.ReadAsAsync<object>();
log.Info(JsonConvert.SerializeObject(data));
// Set name to query string or body data
name = name ?? data?.text;
Response res = new Response();
res.type = "Message";
res.text = $"You said:{name}";
var response = req.CreateResponse(HttpStatusCode.OK);
response.Content = new StringContent(JsonConvert.SerializeObject(res));
response.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
return response;
}
public class Response {
public string type;
public string text;
}

Sending Images to the client from tomcat server

I am building a framework for e-commerce site. I have used jersey to create REST APIs. I need to send images to the clients as per the request.
How can I do so from my application server as Tomcat and jersey as REST API?
Since I am new to this, I have no clue how to send images to an Android client when they are shown as item.
Every resource is identified by the URI, client will ask for a particular image or a bunch of images by quering the URL, So you just need to expose a service, following service is an example to send single image to client.
#GET
#Path("/images/{imageId}")
#Produces("image/png")
public Response downloadImage(#PathParam("imageId") String imageId) {
MultiMediaDB imageDb = new MultiMediaDB();
String filePath = imageDb.getImage(imageId);
File file = new File(filePath);
ResponseBuilder response = Response.ok((Object) file);
response.header("Content-Disposition",
"attachment; filename=\"fileName.png\"");
return response.build();
}
MultiMediaDB is my custom class to get the file location from the DB, you can hardcode it as of now for testing purpose like D:\server_image.png.
You need to mention Content-Disposition as an attachment so that file will not be downloaded, instead attached to the form. In android you just need to read inputstream from a HttpURLConnection object and send that to bitmap as shown below
URL url = new URL(BaseUrl + "/images/" + imageId);
HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection();
urlConnection.connect();
iStream = urlConnection.getInputStream();
bitmap = BitmapFactory.decodeStream(iStream);
The you can set that bitmap to imageview or what ever you have as a container.

Resources