Get oauth2 access_token owner email address in ruby? - ruby-on-rails

I am successfully using Oauth2 gem to get an access tokens from Google accounts. But I want to get the email address which produced the token (I mean if I used this account "myemail#gmail.com" to get the access_token, how can I know the email address from the token?). Or can I get the email while obtaining the token?

Usually you can get that email with the response for any API call. For contacts API, by example, there is an entry called author in the response: https://developers.google.com/google-apps/contacts/v3/#retrieving_all_contacts
Anyway, I'm not sure which gem you're using. You can use oauth2, doorkeeper or cloudsponge.

Related

JavaMail oauth requires full mail scope for sending email via gmail

JavaMail oauth requires full mail scope for sending email via gmail.
Used scope "https://www.googleapis.com/auth/gmail.send" for authorization.
then get refresh and access token.
Java mail:
transport.connect(host, port, username, access_token);
Got error:
Oauth asks for more
After changing scope to "https://mail.google.com/" for authorization, it works. But this scope will authorize
send, read, delete all emails.
This scope is too much, no difference from password authentication.

Getting the username (at least user's email address), after obtaining the access token for EWS.AccessAsUser.All

We are getting an access token for scopes: 'https://outlook.office.com/EWS.AccessAsUser.All offline_access'.
Unfortunately, the response for 'https://login.microsoftonline.com/common/oauth2/v2.0/token' doesn't contain id_token or something like this.
So the question is how to get username (an email address or unique_name) using the access token.
Oh, I just had to add "openid email" to scopes to get id_token which contains email address.

Retrieve Slack bot token with devise and omniauth-slack on Rails

I'm authenticating Slack users with devise and omniauth-slack on Rails and I'm currently able to retrieve user's token as expected.
The problem is that, with Slack addition to include "bot" scoping, we also need to retrieve the bot's token and for that, we must retrieve the first (and unique) token response --as explained in their documentation
Is there a way to access this response in our Devise::OmniauthCallbacksController ?
If not, is there another way I could retrieve this response?

How to get userId from access token for Gmail OAuth2.0

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>.
You can do a Users: getProfile-request immediataly as they log in for the first time, like so:
GET https://www.googleapis.com/gmail/v1/users/me/profile?access_token=<ACCESS_TOKEN_OF_THE_LOGGED_IN_USER>
Response:
{
"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.
}
Then, when you get a push request via watch(), you can use the historyId in the response above to see what has happened.

Extracting Gmail username with OAuth access token

I'm using OAuth to get a user to grant me access to their Gmail IMAP account. I can successfully get an access token, and need to know what endpoint I can access to get the authenticated user's e-mail address.
And you can test this feed using Google's OAuth Playground, as I just did.
In step one, paste the scope:
https://www.googleapis.com/auth/userinfo#email
And in step six, paste the feed URI:
https://www.googleapis.com/userinfo/email
This should help in adapting the feed for use in your own application.
You can get user's username and email with endpoint: https://www.googleapis.com/userinfo/email with scope: https://www.googleapis.com/auth/userinfo#email.

Resources