1

What I'm trying to do

I'm trying to create a web app that would fetch emails from a user's Gmail. I understand that I would require authentication via Google OAuth 2.0.

What I'm trying to do is setting a watch() request on an inbox. Then, when the watched inbox gets an email, I want to get notified. I'm using Google's PubSub API.

What I've done

I followed the steps given in the Gmail OAuth documentation, but am confused as to how to store multiple access tokens for multiple users.

So when the user first logs in using their Gmail account, I generate a code which I exchange for an access token. Then I store this in a database. However, when I get notified about an email, I only receive from the API the user's email address. I would like to do some further API calls for this user. So now I would need to retrieve the token for the specific user. But...

The problem

... How do I store access tokens by email? I only have access to their token once they login/authorize. Is there any way of retrieving the email address from an access token? I could then store the access tokens as key-value pairs of <email address>-><access token>.

4

1 回答 1

2

您可以在用户第一次登录时立即执行用户:getProfile -request,如下所示:

GET https://www.googleapis.com/gmail/v1/users/me/profile?access_token=<ACCESS_TOKEN_OF_THE_LOGGED_IN_USER>

回复:

{
 "emailAddress": "example@gmail.com", // Here is the user's email address.
 "messagesTotal": 6446,
 "threadsTotal": 4495,
 "historyId": "570232" // Here is the current historyId of his account.
}

然后,当您通过 收到推送请求时watch(),您可以使用historyId上面响应中的 来查看发生了什么

于 2015-09-09T10:05:48.180 回答