Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I get a URL link to a Gmail draft using the Gmail API?

The new Gmail API allows us to create and view drafts, but is there a way to get a URL link to view the draft?

I can manually create the link using the draft's ThreadId with something like this:

https://mail.google.com/mail/u/0/#drafts?compose={ThreadId}

But that is somewhat fragile if Google decides to change how those URLs are structured. I also don't know if the URL will be different for people in other countries. The 0 will also change depending on how many accounts you are logged into in the browser. Is there a better way to get this link than creating it manually like I am?

Also, is there a way to pass authentication information along with the URL so that user is logged in when going to the webpage? I'm assuming that there is no way to do this, but I wanted to check. If a user is not logged in, the draft link brings up the login page and the draft is not displayed after logging in.

like image 478
Brett Lesnau Avatar asked Jun 28 '14 00:06

Brett Lesnau


2 Answers

No, creating the URL manually is currently your best option. You can drop the u/0/ if you want and Gmail will automatically use the first authenticated account.

No, there is not a way for you to automatically sign a user into Gmail.

like image 150
abraham Avatar answered Sep 21 '22 20:09

abraham


For exposing URL to a draft created by API, this works:

...
final Gmail.Users.Drafts.Create request = gmailService.users().drafts().create("me", content);
final Draft response = request.execute();
final String url = "https://mail.google.com/mail/ca/u/0/#drafts/" 
             + response.getMessage().getThreadId() 
...

When the user clicks on it, gmail will translate (and redirect) to a different URL, but opens the correct draft. I however did not find mention of this in the docs, so it might be an unsupported feature that stops working one day.

Credits: @Chris Wood from this SO question stackoverflow.com/q/50124112/455449 (see his comment below question)


Concerning passing authentication info:

  • passing account id is probably not possible
  • passing password in url would be a security nonsense (so I trust it is not possible)
like image 34
Petr Kozelka Avatar answered Sep 20 '22 20:09

Petr Kozelka