Skip to content

Instantly share code, notes, and snippets.

@skycocker
Last active March 30, 2024 13:32
Show Gist options
  • Save skycocker/ba67e6756131fb43cf4963e024158be1 to your computer and use it in GitHub Desktop.
Save skycocker/ba67e6756131fb43cf4963e024158be1 to your computer and use it in GitHub Desktop.
Get basic user info from Google Oauth V2 api (since google is an extraordinary piece of shit when it comes to documenting their stuff) (or making admin consoles)
gem 'google-api-client'
require 'google/apis/oauth2_v2'
google = Google::Apis::Oauth2V2::Oauth2Service.new
signet = Signet::OAuth2::Client.new(
authorization_uri: 'https://accounts.google.com/o/oauth2/auth',
token_credential_uri: 'https://www.googleapis.com/oauth2/v3/token',
client_id: 'YOUR_CLIENT_ID',
# in my case, the code from line 17 is obtained from an android app,
# therefore the client_secret is an empty string.
# If you have a client_secret (for example if you have registered your google app as web application),
# make sure to provide it here.
client_secret: '',
scope: 'email profile', # if you need only the name and gender, feel free to remove the 'email' part
redirect_uri: 'http://localhost/callback', # it has to match the redirect_uri provided when obtaining the code from line 17
)
signet.code = '4/R0V7NkJ_kD8Mj-MVFjfsTofPbB78oMSEjyTUAoFqpew' # https://developers.google.com/identity/protocols/OAuth2WebServer#handlingresponse - modify the link from section "Sample OAuth 2.0 server response" according to your project, then use the code passed after a redirect
signet.fetch_access_token!
google.authorization = signet
google.get_userinfo_v2
# => #<Google::Apis::Oauth2V2::Userinfoplus:0x007f916603a350 @email="[email protected]", @family_name="Shit", @gender="male", @given_name="Shitter", @id="696969696969696969", @link="https://plus.google.com/696969696969696969", @locale="pl", @name="Shitter Shit", @picture="https://lh3.googleusercontent.com/-XXXXXXX/AAAAAAAAAAD/XCXCCCCCCC/42954939/photo.jpg", @verified_email=true>
@Struki84
Copy link

Thank you! Agreed, Google docs is crap, I've been looking for an example like this for weeks...

@skycocker
Copy link
Author

@Struki84 glad I could’ve helped 🙂

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment