Brett Lesnau
Brett Lesnau

Reputation: 218

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.

Upvotes: 7

Views: 3099

Answers (2)

Petr Kozelka
Petr Kozelka

Reputation: 7980

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)

Upvotes: 1

abraham
abraham

Reputation: 47833

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.

Upvotes: 2

Related Questions