Skip to content

Commit

Permalink
Upgrade oauth2client to v2
Browse files Browse the repository at this point in the history
  • Loading branch information
philipwalton committed Mar 12, 2016
1 parent e589be2 commit 9107738
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 22 deletions.
13 changes: 4 additions & 9 deletions lib/service_account.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@
# limitations under the License.


import json
from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.service_account import ServiceAccountCredentials


# The scope for the OAuth2 request.
Expand All @@ -40,13 +39,9 @@ def get_credentials():
if __credentials:
return __credentials
else:
# Loads the key file's private data.
with open(KEY_FILEPATH) as key_file:
key_data = json.load(key_file)

# Constructs a credentials objects from the key data and OAuth2 scope.
__credentials = SignedJwtAssertionCredentials(
key_data['client_email'], key_data['private_key'], SCOPE)
# Constructs a credentials objects from the key file and OAuth2 scope.
__credentials = ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILEPATH, SCOPE)

return __credentials

Expand Down
17 changes: 4 additions & 13 deletions templates/embed-api/server-side-authorization.html
Original file line number Diff line number Diff line change
Expand Up @@ -59,27 +59,18 @@ <h3>Step 3: Use the JSON key data to request an access token</h3>
{%- filter forceescape -%}
# service-account.py

import json
from oauth2client.client import SignedJwtAssertionCredentials
from oauth2client.service_account import ServiceAccountCredentials

# The scope for the OAuth2 request.
SCOPE = 'https://www.googleapis.com/auth/analytics.readonly'

# The location of the key file with the key data.
KEY_FILEPATH = 'path/to/json-key.json'

# Load the key file's private data.
with open(KEY_FILEPATH) as key_file:
_key_data = json.load(key_file)

# Construct a credentials objects from the key data and OAuth2 scope.
_credentials = SignedJwtAssertionCredentials(
_key_data['client_email'], _key_data['private_key'], SCOPE)

# Defines a method to get an access token from the credentials object.
# The access token is automatically refreshed if it has expired.
# Defines a method to get an access token from the ServiceAccount object.
def get_access_token():
return _credentials.get_access_token().access_token
return ServiceAccountCredentials.from_json_keyfile_name(
KEY_FILEPATH, SCOPE).get_access_token().access_token
{%- endfilter -%}
</pre>

Expand Down

0 comments on commit 9107738

Please sign in to comment.