根据 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