【发布时间】:2017-09-24 23:04:55
【问题描述】:
- 语言:C#
- 框架:.NET CORE 1.1.1
- Nuget:Google.Apis.YouTube.v3 1.25.0.760 和 Google.Apis 1.25.0
嗨,
我想做什么
我需要获取访问令牌(带有刷新令牌)才能在我的用户 YouTube 帐户上上传视频。
什么不起作用
当我通过 Google API 请求令牌时,它在 localhost 中工作,但不在生产环境中。在生产中,当我将用户重定向到 Google 的 OAuth 服务器时出现redirect_uri_mismatch 错误。
我是如何做到的
我在 Google API 控制台中创建了一个控制台令牌,因为我需要检索一个离线令牌(如果我在 Google API 控制台上创建一个 Web 应用程序令牌,则无法获取离线令牌)。
在我的代码中:我使用此 url 将用户重定向到 Google 的 OAuth 服务器:
https://accounts.google.com/o/oauth2/auth?client_id=XXXXXX.apps.googleusercontent.com&redirect_uri=MyRedirectUri&scope=https://www.googleapis.com/auth/youtube.readonly&response_type=code&access_type=offline
为了让文章更容易阅读:
- 基本网址:https://accounts.google.com/o/oauth2/auth
- client_id : 我的客户 ID
- redirect_uri : http://localhost:43443/Account/Oauth (dév) 和 http://xxxxxx.azurewebsites.net/Account/Oauth (prod)
- 范围:https://www.googleapis.com/auth/youtube.readonly
- response_type :code
- access_type:离线
在开发中,它正在工作。在生产中,它不起作用 (redirect_uri_mismatch)
我还尝试将 redirect_uri 更改为 urn:ietf:wg:oauth:2.0:oob 就像 the answer of this post 说的那样。但它只是打开一个带有复制/粘贴代码的页面。
PS:我通知说,当我从 Google Console API 下载我的“client_secrets.json”时,下载文件中有一个 redirect_uris,其中包含以下值:["urn:ietf:wg:oauth:2.0:oob","http://localhost"]。但在 Google API 控制台中我无法修改此参数。
PS2:我不使用 .NET API 来请求令牌,因为 as the answer to this post say(正如我之前尝试过的那样),无法使用 .NET API 获取离线令牌。
感谢您的帮助
编辑 1: This post 不回答我的问题,因为他使用“Web 应用程序令牌”进行一次拍摄请求。就我而言,我在无法设置重定向的地方使用“控制台令牌”(在 Google API 控制台中称为“其他”)。我使用“控制台令牌”来获取离线令牌。
【问题讨论】:
-
在您提到的帖子中,他使用“Web 应用程序令牌”进行一次拍摄请求。就我而言,我在无法设置重定向的地方使用“控制台令牌”(在 Google API 控制台中称为“其他”)。我使用“控制台令牌”来获取离线令牌。
标签: google-oauth .net-core google-api-dotnet-client youtube-data-api