请求令牌时,Google API刷新令牌为None

2024-03-29 10:31:32 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经按照下面的指南为我的应用程序获取了一个Google Ads API刷新令牌

https://github.com/googleads/googleads-python-lib/wiki/API-access-on-behalf-of-your-clients-(web-flow)

使用下面的脚本,一切正常,但响应只有一个访问令牌,而刷新令牌则没有

from googleads import oauth2
import google.oauth2.credentials
import google_auth_oauthlib.flow


# Initialize the flow using the client ID and secret downloaded earlier.
# Note: You can use the GetAPIScope helper function to retrieve the
# appropriate scope for AdWords or Ad Manager.
flow = google_auth_oauthlib.flow.Flow.from_client_secrets_file(
    'client_secret.json',
    [oauth2.GetAPIScope('adwords')])
# Indicate where the API server will redirect the user after the user completes
# the authorization flow. The redirect URI is required.
flow.redirect_uri = 'https://www.example.com'

# Generate URL for request to Google's OAuth 2.0 server.
# Use kwargs to set optional request parameters.
authorization_url, state = flow.authorization_url(
    # Enable offline access so that you can refresh an access token without
    # re-prompting the user for permission. Recommended for web server apps.
    access_type='offline',
    # Enable incremental authorization. Recommended as a best practice.
    include_granted_scopes='true',
    # approval_prompt='force'
)
print("\n" + authorization_url)
print("\nVisit the above URL and grant access. You will be redirected. Get the 'code' from the query params of the redirect URL.")

auth_code = input('\nCode: ').strip()

flow.fetch_token(code=auth_code)
credentials = flow.credentials
print(credentials.__dict__)

Tags: thefromimportauthapiforaccessgoogle
1条回答
网友
1楼 · 发布于 2024-03-29 10:31:32

问题似乎是我以前已经完成了这些步骤

解决方案是将approval_prompt='force'包含在flow.authorization_url()中。以这种方式生成authorization_url之后,响应还包括一个刷新令牌

相关问题 更多 >