-
Notifications
You must be signed in to change notification settings - Fork 48
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Simplify service account user code #90
Comments
Google::APIClient::JWTAsserter is deprecated (http://www.rubydoc.info/github/google/google-api-ruby-client/Google/APIClient/JWTAsserter) and service accounts are now supported directly in Signet. The above code can be written as: def service_account_user(scope = "https://www.googleapis.com/auth/analytics.readonly")
key = Google::APIClient::KeyUtils.load_from_pkcs12("YOUR_PRIVATE_KEY_FILENAME", "notasecret")
auth_client = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: 'YOUR_API_EMAIL_ADDRESS@developer.gserviceaccount.com',
signing_key: key,
sub: 'YOUR_ANALYTICS_EMAIL@example.com')
access_token = auth_client.fetch_access_token!
oauth_client = OAuth2::Client.new("", "", {
authorize_url: 'https://accounts.google.com/o/oauth2/auth',
token_url: 'https://accounts.google.com/o/oauth2/token'
})
token = OAuth2::AccessToken.new(oauth_client, access_token["access_token"], expires_in: 1.hour)
user = Legato::User.new(token)
# after an hour or so
user.access_token.expired?
end The benefit of this way is that the key If
I think the docs should be updated, it will save many hours searching. |
What is |
https://developers.google.com/identity/protocols/OAuth2ServiceAccount, that whole section of the Wiki may just need a rewrite. |
Yes, signet is a google gem (https://github.com/google/signet). Its a dependency of google-api-client gem (https://rubygems.org/gems/google-api-client) and it is used to handle authorization. The google-api-client docs suggests to use the new way. See https://github.com/google/google-api-ruby-client#authorization |
google-api-client or googleauth (a dependency of google-api-client) are not needed. The code can be simplified by just using the signet gem that is a dependency of googleauth. require 'signet/oauth_2/client'
def service_account_user(scope = 'https://www.googleapis.com/auth/analytics.readonly')
key = OpenSSL::PKCS12.new(File.read('YOUR_PRIVATE_KEY_FILENAME'), 'notasecret').key
auth_client = Signet::OAuth2::Client.new(
token_credential_uri: 'https://accounts.google.com/o/oauth2/token',
audience: 'https://accounts.google.com/o/oauth2/token',
scope: scope,
issuer: 'YOUR_API_EMAIL_ADDRESS@developer.gserviceaccount.com',
signing_key: key,
sub: 'YOUR_API_EMAIL_ADDRESS@developer.gserviceaccount.com')
access_token = auth_client.fetch_access_token!
oauth_client = OAuth2::Client.new('', '', {
authorize_url: 'https://accounts.google.com/o/oauth2/auth',
token_url: 'https://accounts.google.com/o/oauth2/token'
})
token = OAuth2::AccessToken.new(oauth_client, access_token['access_token'], expires_in: access_token['expires_in'])
user = Legato::User.new(token)
# after an hour or so
user.access_token.expired?
end |
Updated the wiki. https://github.com/tpitale/legato/wiki/OAuth2-and-Google |
Hi there, I'm getting key = OpenSSL::PKCS12.new(config.fetch('private_key'), 'notasecret').key where {
"type": "service_account",
"project_id": "xxx",
"private_key_id": "xxx",
"private_key": "-----BEGIN PRIVATE KEY-----
xxxxxxxxxxxxxxxxxxxx
-----END PRIVATE KEY-----\n",
"client_email": "xxxxx@appspot.gserviceaccount.com",
"client_id": "xxxxx",
"auth_uri": "https://accounts.google.com/o/oauth2/auth",
"token_uri": "https://accounts.google.com/o/oauth2/token",
"auth_provider_x509_cert_url": "https://www.googleapis.com/oauth2/v1/certs",
"client_x509_cert_url": "xxxx"
} Do you recognize the error? |
I just tried to create a new service account and there is a new setting for JSON vs. P12. I'm guessing it needs to be P12 to work with |
I'm looking at https://github.com/google/google-auth-library-ruby now to see what they're doing with JSON. |
To use JSON you're going to have to follow these instructions https://developers.google.com/identity/protocols/application-default-credentials and use the |
It's essentially looking for the path to the JSON file to be in the env variable |
Digging in more, looks like |
Ah! Here is the secret: https://github.com/google/google-auth-library-ruby/blob/37ed189b2e5165243918fcde127ebe323e9a06b5/lib/googleauth/service_account.rb#L56-L70 The JSON file has an RSA key, not a P12 key. So use |
I've updated the wiki with this new information. |
Thank's @tpitale, you rock! |
The code in https://github.com/tpitale/legato/wiki/OAuth2-and-Google#service-accounts can be simplified (no need to initialize Google::APIClient.new)
The text was updated successfully, but these errors were encountered: