Google API: getting Credentials from refresh token with oauth2client.client

前端 未结 10 1889
失恋的感觉
失恋的感觉 2020-11-29 05:06

I am using googles official oauth2client.client to access the google plus api. I have a refresh token (that does not expire) stored in a database, and need to recreate the

10条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-29 05:25

    You can construct an OAuth2Credentials instance directly like this:

    import httplib2
    from oauth2client import GOOGLE_REVOKE_URI, GOOGLE_TOKEN_URI, client
    
    CLIENT_ID = ''
    CLIENT_SECRET = ''
    REFRESH_TOKEN = ''
    
    credentials = client.OAuth2Credentials(
        access_token=None,  # set access_token to None since we use a refresh token
        client_id=CLIENT_ID,
        client_secret=CLIENT_SECRET,
        refresh_token=REFRESH_TOKEN,
        token_expiry=None,
        token_uri=GOOGLE_TOKEN_URI,
        user_agent=None,
        revoke_uri=GOOGLE_REVOKE_URI)
    
    credentials.refresh(httplib2.Http())  # refresh the access token (optional)
    print(credentials.to_json())
    http = credentials.authorize(httplib2.Http())  # apply the credentials
    

提交回复
热议问题