Google API Authorization (Service Account) Error: HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request
Google API Authorization (Service Account) Error: HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request
我正在尝试使用 Python 连接到 YouTube 分析工具 API。当我在凭证选项卡中转到 Google 的开发人员控制台时,我单击 Create credentials
下拉菜单和 select Help me choose
。我单击要使用的 API (YouTube Analytics API
)、我将从哪里调用 (Other non-UI (e.g. cron job, daemon)
)、我将访问哪些数据 (Application Data
),以及然后我是否使用 Google App Engine (no
)。我单击按钮查看我需要哪些凭据,它告诉我 You alread have credentials that are suitable for this purpose
.
我有一个 Service account
用于连接到 Google Search Console API 以访问我公司拥有的多个站点的数据。因为我们有多个站点,所以我使用基于我的电子邮件地址的委托凭据。这是我用来向 Search Console 进行身份验证的代码 API:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
webmasters_service = build('webmasters', 'v3', http=http_auth)
现在,我尝试对 YouTube 分析使用类似的方法 API,但我收到此错误:HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.
。这是我的代码:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
START_DATE = "2016-09-01"
END_DATE = "2016-09-30"
scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
youtube_service = build('youtubeAnalytics', 'v1', http=http_auth)
analytics_query_response = youtube_service.reports().query(
ids="channel==<my-youtube-channel-id>",
metrics="views",
start_date=START_DATE,
end_date=END_DATE
).execute()
keyfile.json
是我用来连接到 Search Console API 的同一个文件(包含服务帐户凭据)。我什至尝试创建一个新的服务帐户并使用这些凭据,但我没有任何运气。是的,我已经在开发者控制台中启用了所有 YouTube API。
你知道我为什么会收到 HttpAccessTokenRefreshError: unauthorized_client: ...
错误吗?
编辑:以前我使用 OAuth ID 而不是服务帐户,当我 运行 我的脚本出现浏览器选项卡时,我必须选择一个帐户。我有两个选择:一个是我的电子邮件,另一个是我被添加为管理员的 YouTube 帐户。您是否认为我收到该错误是因为我使用我的电子邮件地址生成凭据(而不是 YouTube 帐户)?
edit2:看来 YouTube 帐户可能不属于我们的 Google Apps 域。因此,这可能就是我无法授权使用我的电子邮件地址的原因,即使我已成为该电子邮件地址的 YouTube 帐户的管理员。
我在此 documentation 上发现您无法将服务帐户用于 YouTube 分析 API。
这里声明:
The service account flow supports server-to-server interactions that
do not access user information. However, the YouTube Analytics API
does not support this flow. Since there is no way to link a Service
Account to a YouTube account, attempts to authorize requests with this
flow will generate an error.
我也在这个 thread 一位 Google 员工的回答中找到了它。
有关详细信息,请查看此 SO question。
我正在尝试使用 Python 连接到 YouTube 分析工具 API。当我在凭证选项卡中转到 Google 的开发人员控制台时,我单击 Create credentials
下拉菜单和 select Help me choose
。我单击要使用的 API (YouTube Analytics API
)、我将从哪里调用 (Other non-UI (e.g. cron job, daemon)
)、我将访问哪些数据 (Application Data
),以及然后我是否使用 Google App Engine (no
)。我单击按钮查看我需要哪些凭据,它告诉我 You alread have credentials that are suitable for this purpose
.
我有一个 Service account
用于连接到 Google Search Console API 以访问我公司拥有的多个站点的数据。因为我们有多个站点,所以我使用基于我的电子邮件地址的委托凭据。这是我用来向 Search Console 进行身份验证的代码 API:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
scopes = ['https://www.googleapis.com/auth/webmasters.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
webmasters_service = build('webmasters', 'v3', http=http_auth)
现在,我尝试对 YouTube 分析使用类似的方法 API,但我收到此错误:HttpAccessTokenRefreshError: unauthorized_client: Unauthorized client or scope in request.
。这是我的代码:
from httplib2 import Http
from oauth2client.service_account import ServiceAccountCredentials
from apiclient.discovery import build
START_DATE = "2016-09-01"
END_DATE = "2016-09-30"
scopes = ['https://www.googleapis.com/auth/yt-analytics.readonly']
credentials = ServiceAccountCredentials.from_json_keyfile_name('keyfile.json', scopes=scopes)
delegated_credentials = credentials.create_delegated('<my-email>')
http_auth = delegated_credentials.authorize(Http())
youtube_service = build('youtubeAnalytics', 'v1', http=http_auth)
analytics_query_response = youtube_service.reports().query(
ids="channel==<my-youtube-channel-id>",
metrics="views",
start_date=START_DATE,
end_date=END_DATE
).execute()
keyfile.json
是我用来连接到 Search Console API 的同一个文件(包含服务帐户凭据)。我什至尝试创建一个新的服务帐户并使用这些凭据,但我没有任何运气。是的,我已经在开发者控制台中启用了所有 YouTube API。
你知道我为什么会收到 HttpAccessTokenRefreshError: unauthorized_client: ...
错误吗?
编辑:以前我使用 OAuth ID 而不是服务帐户,当我 运行 我的脚本出现浏览器选项卡时,我必须选择一个帐户。我有两个选择:一个是我的电子邮件,另一个是我被添加为管理员的 YouTube 帐户。您是否认为我收到该错误是因为我使用我的电子邮件地址生成凭据(而不是 YouTube 帐户)?
edit2:看来 YouTube 帐户可能不属于我们的 Google Apps 域。因此,这可能就是我无法授权使用我的电子邮件地址的原因,即使我已成为该电子邮件地址的 YouTube 帐户的管理员。
我在此 documentation 上发现您无法将服务帐户用于 YouTube 分析 API。
这里声明:
The service account flow supports server-to-server interactions that do not access user information. However, the YouTube Analytics API does not support this flow. Since there is no way to link a Service Account to a YouTube account, attempts to authorize requests with this flow will generate an error.
我也在这个 thread 一位 Google 员工的回答中找到了它。
有关详细信息,请查看此 SO question。