コード例 #1
0
    def ensure_token(self):
        if not self.token:
            self._load_token()

        if not self.token:
            self.session.scope = GooglePhotos.SCOPE
            self.session.redirect_uri = GooglePhotos.REDIRECT_URI
            authorization_url, _ = self.session.authorization_url(
                GooglePhotos.AUTH_URI,
                # offline for refresh token, # force to always make user click authorize
                access_type="offline",
                prompt="select_account")
            webbrowser.open(authorization_url)
            # Get the authorization verifier code from the callback url
            code = input('Paste the code here:')
            self.token = self.session.fetch_token(
                GooglePhotos.TOKEN_URI,
                client_secret=self._client_secret,
                code=code)
            self._save_token()

        if self.token['expires_at'] <= time():
            self.session.token = self.token
            self.token = self.session.refresh_token(
                GooglePhotos.TOKEN_URI,
                client_secret=self._client_secret,
                client_id=GooglePhotos.CLIENT_ID)
            self._save_token()

        self.session.token = self.token
        self.api = build('photoslibrary',
                         'v1',
                         credentials=credentials_from_session(self.session))
コード例 #2
0
ファイル: try_google_auth.py プロジェクト: kimiyuki/gatools
def auth_flow():
    auth = OAuth2Session(client_id=CLIENT_ID,
                         scope=OAUTH_SCOPE,
                         redirect_uri=REDIRECT_URI,
                         auto_refresh_url=token_url)

    # Redirect user to Google for authorization
    authorization_url, state = auth.authorization_url(
        authorization_base_url,
        # offline for refresh token
        # force to always make user click authorize
        access_type="offline",
        prompt="select_account")
    print('Please go here and authorize,', authorization_url)

    # Get the authorization verifier code from the callback url
    #redirect_response = raw_input('Paste the full redirect URL here:')
    code = input('Paste the code:')

    # Fetch the access token
    auth.fetch_token(token_url, code=code, client_secret=CLIENT_SECRET)
    #authorization_response=redirect_response)
    from google_auth_oauthlib.helpers import credentials_from_session
    google_auth_credentials = credentials_from_session(auth)
    import pickle
    pickle.dump(google_auth_credentials, open('g_auth_cred.picle', 'wb'))
    return google_auth_credentials
コード例 #3
0
def sampleentry():
    oauth2_session = OAuth2Session(client_id, token=session[OAUTH2_TOKEN])
    google_auth_credentials = credentials_from_session(oauth2_session)
    datacatalog = datacatalog_v1.DataCatalogClient(
        credentials=google_auth_credentials)

    resource_name = '//bigquery.googleapis.com/projects/{}/datasets/{}' \
                    '/tables/{}' \
        .format('bigquery-public-data', 'covid19_usafacts', 'summary')

    data_catalog_entry = datacatalog.lookup_entry(
        request={'linked_resource': resource_name})
    return str(data_catalog_entry)
コード例 #4
0
def entry(project_id, dataset, table):
    oauth2_session = OAuth2Session(client_id, token=session[OAUTH2_TOKEN])
    google_auth_credentials = credentials_from_session(oauth2_session)
    datacatalog = datacatalog_v1.DataCatalogClient(
        credentials=google_auth_credentials)

    resource_name = '//bigquery.googleapis.com/projects/{}/datasets/{}' \
                    '/tables/{}' \
        .format(project_id, dataset, table)

    data_catalog_entry = datacatalog.lookup_entry(
        request={'linked_resource': resource_name})
    return str(data_catalog_entry)
コード例 #5
0
def test_credentials_from_session(session):
    session.token = {
        'access_token': mock.sentinel.access_token,
        'refresh_token': mock.sentinel.refresh_token
    }

    credentials = helpers.credentials_from_session(session,
                                                   CLIENT_SECRETS_INFO['web'])

    assert credentials.token == mock.sentinel.access_token
    assert credentials._refresh_token == mock.sentinel.refresh_token
    assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
    assert (credentials._client_secret == CLIENT_SECRETS_INFO['web']
            ['client_secret'])
    assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
コード例 #6
0
def test_credentials_from_session(session):
    session.token = {
        "access_token": mock.sentinel.access_token,
        "refresh_token": mock.sentinel.refresh_token,
        "id_token": mock.sentinel.id_token,
        "expires_at": 643969200.0,
    }

    credentials = helpers.credentials_from_session(session,
                                                   CLIENT_SECRETS_INFO["web"])

    assert credentials.token == mock.sentinel.access_token
    assert credentials.expiry == datetime.datetime(1990, 5, 29, 8, 20, 0)
    assert credentials._refresh_token == mock.sentinel.refresh_token
    assert credentials.id_token == mock.sentinel.id_token
    assert credentials._client_id == CLIENT_SECRETS_INFO["web"]["client_id"]
    assert credentials._client_secret == CLIENT_SECRETS_INFO["web"][
        "client_secret"]
    assert credentials._token_uri == CLIENT_SECRETS_INFO["web"]["token_uri"]
コード例 #7
0
def test_credentials_from_session(session):
    session.token = {
        'access_token': mock.sentinel.access_token,
        'refresh_token': mock.sentinel.refresh_token,
        'id_token': mock.sentinel.id_token,
        'expires_at': 643969200.0
    }

    credentials = helpers.credentials_from_session(session,
                                                   CLIENT_SECRETS_INFO['web'])

    assert credentials.token == mock.sentinel.access_token
    assert credentials.expiry == datetime.datetime(1990, 5, 29, 8, 20, 0)
    assert credentials._refresh_token == mock.sentinel.refresh_token
    assert credentials.id_token == mock.sentinel.id_token
    assert credentials._client_id == CLIENT_SECRETS_INFO['web']['client_id']
    assert (credentials._client_secret == CLIENT_SECRETS_INFO['web']
            ['client_secret'])
    assert credentials._token_uri == CLIENT_SECRETS_INFO['web']['token_uri']
コード例 #8
0
def test_bad_credentials(session):
    with pytest.raises(ValueError):
        helpers.credentials_from_session(session)
コード例 #9
0
def zones(project_id):
    oauth2_session = OAuth2Session(client_id, token=session[OAUTH2_TOKEN])
    google_auth_credentials = credentials_from_session(oauth2_session)
    zones = list_instances(google_auth_credentials, project_id)
    return jsonify(zones)
コード例 #10
0
ファイル: siteData.py プロジェクト: kimiyuki/gatools
 def _authorize(self, auth, code):
     auth.fetch_token(TOKEN_URL, code=code, client_secret=CLIENT_SECRET)
     from google_auth_oauthlib.helpers import credentials_from_session
     #TODO detect why return value does not include client_id, sercet, token_uri
     # temporarily I use google.oauth2.credentials.Credentials constructor instead.
     self.cred = credentials_from_session(auth)