【问题标题】:Does the "refresh_token" refresh "access_token" with "client_secrets.json" in the background?“refresh_token”是否在后台使用“client_secrets.json”刷新“access_token”?
【发布时间】:2014-03-04 01:02:46
【问题描述】:

最后,我通过新的 NuGet 让它在 Youtube Data API V3.0 上运行。非常感谢。我还有一个关于“access_token”的问题。

我们只能使用“access_token”一个小时。我们是否必须每小时刷新此“access_token”,还是“refresh_token”在后台使用“client_secrets.json”自行生成?如果我必须自己刷新它,你能告诉我我可以使用的代码或文档吗?

非常感谢。

如果我使用了错误的语法,我深表歉意。

【问题讨论】:

  • 根据我使用 oauth 2.0 的经验,您需要在 access_token 过期后使用 refresh_token 刷新 access_token。不过,我对 Youtube 的数据 API 了解不多。如果它实现了 oauth 2.0,您将需要一个令牌 uri,并且您需要将您的 refresh_token 发布到该 uri 并将 grant_type 设置为 refresh_token。
  • 谢谢卡梅伦。我刚从 Jeff Posnick 那里得到了答复。这是答案“客户端库应该负责自动刷新 OAuth 2 令牌。如果您发现任何问题,请通过developers.google.com/api-client-library/dotnet/support 让库的维护者知道”但是客户端库没有处理所有代币。播放列表中有类似“next_token”的内容。我会问支持。

标签: c# .net youtube-api


【解决方案1】:

根据 Google 的 Youtube Data API v3 上的文档,您需要实现自己的 oauth 客户端。我假设您已经注册了您的应用程序并拥有一个 client_id 和 client_secret。向 Google 的 API 发出请求时需要这些。

首先,您需要加载 https://accounts.google.com/o/oauth2/auth,因为那是 Google 的 oauth2 提供程序。示例请求可能如下所示:

https://accounts.google.com/o/oauth2/auth?
  client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
  redirect_uri=http%3A%2F%2Flocalhost%2Foauth2callback&
  scope=https://www.googleapis.com/auth/youtube&
  response_type=code&
  access_type=offline 

示例响应:

http://localhost/oauth2callback?code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf

假设您的用户接受来自 Google 的访问权限,那么您将需要交换从 Google 返回的授权代码以获取刷新和访问令牌。

您需要 POST 到此地址: https://accounts.google.com/o/oauth2/token

您的帖子数据将包括从先前响应返回的代码、您的 client_id、您的 client_secret、您的 redirect_uri 和 grant_type。

示例帖子数据:

code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&
client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&
client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&
redirect_uri=http://localhost/oauth2callback&
grant_type=authorization_code

示例响应:

{
  "access_token" : "ya29.AHES6ZTtm7SuokEB-RGtbBty9IIlNiP9-eNMMQKtXdMP3sfjL1Fc",
  "token_type" : "Bearer",
  "expires_in" : 3600,
  "refresh_token" : "1/HKSmLFXzqP0leUihZp2xUt3-5wkU7Gmu2Os_eBnzw74"
}

然后您应该存储您的 access_token 和 refresh_token 以备后用。 access_token 将在 3600 秒(1 小时)后过期。

要刷新您的访问令牌,请发布如下内容:

client_id=21302922996.apps.googleusercontent.com&
client_secret=XTHhXh1SlUNgvyWGwDk1EjXB&
refresh_token=1/6BMfW9j53gdGImsixUH6kU5RsR4zwI9lUVX-tqf8JXQ&
grant_type=refresh_token

完整的开发者文档可在此处获得:
https://developers.google.com/youtube/v3/guides/authentication

【讨论】:

  • 谢谢卡梅伦。我认为他们用新的更新改变了一切。客户端库应该负责自动刷新 OAuth 2 令牌。但是客户端库并不处理播放列表中的“next_token”等所有内容。正如 Jess 告诉我的那样,我会向支持人员提出这一要求。我会尽快从他们那里得到答案更新这个问题。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-05-20
  • 2018-12-25
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-01-24
相关资源
最近更新 更多