使用Python连接到YouTube Analytics API时的授权错误
我一直在尝试在几天内连接到YouTube Analytics API。我看到其他帖子中的解决方案与在应用程序类型列表中选择“其他”有关。显然,此选项不再可用。
我创建了两个不同的OAuth 2.0客户端:Web应用程序和桌面。对于第一个,它显示以下错误:
- 错误400:redirect_uri_mismatch:请求中的重定向URI,urn:ietf:wg:wg:oauth:2.0:OOB,只能由客户端ID用于本机应用程序。 Web客户端类型不允许使用。
然后,当我尝试创建本机应用程序(在这种情况下为桌面应用程序)时,它将发送一个新的错误消息:
- 错误400:invalid_request。您无法登录此应用程序,因为它不符合Google的OAuth 2.0策略来确保应用程序安全。您可以让应用程序开发人员知道此应用不符合一个或多个Google验证规则。
我已经阅读了Google的OAuth 2.0策略,但我无法理解为什么不遵守。这不是很具体。
有人遇到过同样的问题吗?这是我一直使用的示例代码:
import googleapiclient.errors
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
#os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtubeAnalytics"
api_version = "v2"
client_secrets_file = "cliente_analytics.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube_analytics = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube_analytics.reports().query(
dimensions="day",
endDate="2021-04-01",
ids="channel==MINE",
maxResults=5,
metrics="likes",
startDate="2021-03-01"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
谢谢
I have been trying to connect during several days to the YouTube Analytics API. I see that the solution in other posts is related about selecting 'Others' in the Application Type list. Apparently, this option is not available anymore.
I have created two different OAuth 2.0 clients: Web Application and Desktop. For the first one, it is displaying the following error:
- Error 400: redirect_uri_mismatch: The redirect URI in the request, urn:ietf:wg:oauth:2.0:oob, can only be used by a Client ID for native application. It is not allowed for the WEB client type.
Then, it is when I try to create a native application (Desktop App in this case), it is sending a new error message:
- Error 400: invalid_request. You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure. You can let the app developer know that this app doesn't comply with one or more Google validation rules.
I have read the Google's OAuth 2.0 policy but I am not able to understand why it doesn't comply. It's not quite specific.
Anyone that has had the same issue? This is the sample code that I have been using:
import googleapiclient.errors
import os
from googleapiclient.discovery import build
from google_auth_oauthlib.flow import InstalledAppFlow
from google.auth.transport.requests import Request
scopes = ["https://www.googleapis.com/auth/youtube.readonly"]
def main():
# Disable OAuthlib's HTTPS verification when running locally.
# *DO NOT* leave this option enabled in production.
#os.environ["OAUTHLIB_INSECURE_TRANSPORT"] = "1"
api_service_name = "youtubeAnalytics"
api_version = "v2"
client_secrets_file = "cliente_analytics.json"
# Get credentials and create an API client
flow = google_auth_oauthlib.flow.InstalledAppFlow.from_client_secrets_file(
client_secrets_file, scopes)
credentials = flow.run_console()
youtube_analytics = googleapiclient.discovery.build(
api_service_name, api_version, credentials=credentials)
request = youtube_analytics.reports().query(
dimensions="day",
endDate="2021-04-01",
ids="channel==MINE",
maxResults=5,
metrics="likes",
startDate="2021-03-01"
)
response = request.execute()
print(response)
if __name__ == "__main__":
main()
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您使用的代码是为安装应用程序设计的,因此
fixing
invalid_request。您无法登录此应用程序,因为它不符合Google的OAuth 2.0策略,以确保应用程序安全。
可能有点欺骗。
The code you are using is designed for an installed application hence the
fixing
invalid_request. You can't sign in to this app because it doesn't comply with Google's OAuth 2.0 policy for keeping apps secure.
Can be a bit tricking.