Last active
March 30, 2024 13:32
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
gem 'google-api-client' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Thank you! Agreed, Google docs is crap, I've been looking for an example like this for weeks...