问题 使用OAuth的Youtube API单用户场景(上传视频)


这个问题已被提出,但从未回答过。

我想写一些PHP脚本,将视频上传到我自己的YouTube帐户。我已经注册了该应用程序并拥有开发人员密钥,客户密钥和客户密钥。

我不需要允许任何用户将视频上传到自己的帐户,因此我不需要完成完整的OAuth流程;特别是我不需要在任何地方重定向任何人:我只需要我的脚本代表我进行身份验证(不代表任何其他人)。

我知道我可以使用ClientLogin身份验证,但我已经在YouTube API文档网站上看到它“不推荐用于新开发”,我担心这意味着它的支持将在不久的将来停止。所以我更喜欢使用OAuth。

Twitter API也使用OAuth,它提供了一种简单的方法,使用您可以在应用程序管理页面上找到的访问令牌,使用应用程序所有者自己的帐户进行身份验证。 如何为Youtube应用程序获取类似的令牌?

谢谢 米


1319
2018-05-01 18:04


起源



答案:


尝试使用OAuth 2.0安装应用程序: http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#OAuth2_Installed_Applications_Flow

首先,注册API以获取client_id。

然后,登录您的Google帐户,输入以下网址,与您的帐户更改client_id。 redirect_uri应设置为“urn:ietf:wg:oauth:2.0:oob”。

https://accounts.google.com/o/oauth2/auth?client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://gdata.youtube.com&response_type=代码&ACCESS_TYPE =离线

然后,您授权自己的应用程序并获取授权码。

然后打开一个终端并输入(更改你的代码,client_id和client_secret):

curl https://accounts.google.com/o/oauth2/token -d "code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"

你会得到如下回应:

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

记住refresh_token,每次运行应用程序时,都需要使用refresh_token获取一个新的access_token。


14
2018-01-16 05:13



它有点像魅力,但值得一提的是1)代码只能使用一次; 2)代码仅有效10分钟(如此匆忙); 3)我不得不设置额外的参数 approval_prompt 如上所述 这里If you need long-lived access to the YouTube API in a web application, you can retrieve one by setting the access_type parameter to offline and the approval_prompt parameter to force in the initial authorization request or your client configuration. - Kaspars Ozols
嘿@DrXCheng感谢您的帮助,因为我可以在您的代码的帮助下获得其他类型应用程序的访问令牌,但我无法获得web服务器应用程序和ios应用程序的accessToken和刷新令牌,所以,请你帮助我。如果我通过ios应用程序然后我没有客户端秘密,那么我如何获得访问令牌和刷新令牌? - Pankaj


答案:


尝试使用OAuth 2.0安装应用程序: http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#OAuth2_Installed_Applications_Flow

首先,注册API以获取client_id。

然后,登录您的Google帐户,输入以下网址,与您的帐户更改client_id。 redirect_uri应设置为“urn:ietf:wg:oauth:2.0:oob”。

https://accounts.google.com/o/oauth2/auth?client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&redirect_uri=urn:ietf:wg:oauth:2.0:oob&scope=https://gdata.youtube.com&response_type=代码&ACCESS_TYPE =离线

然后,您授权自己的应用程序并获取授权码。

然后打开一个终端并输入(更改你的代码,client_id和client_secret):

curl https://accounts.google.com/o/oauth2/token -d "code=4/ux5gNj-_mIu4DOD_gNZdjX9EtOFf&client_id=1084945748469-eg34imk572gdhu83gj5p0an9fut6urp5.apps.googleusercontent.com&client_secret=hDBmMRhz7eJRsM9Z2q1oFBSe&redirect_uri=urn:ietf:wg:oauth:2.0:oob&grant_type=authorization_code"

你会得到如下回应:

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

记住refresh_token,每次运行应用程序时,都需要使用refresh_token获取一个新的access_token。


14
2018-01-16 05:13



它有点像魅力,但值得一提的是1)代码只能使用一次; 2)代码仅有效10分钟(如此匆忙); 3)我不得不设置额外的参数 approval_prompt 如上所述 这里If you need long-lived access to the YouTube API in a web application, you can retrieve one by setting the access_type parameter to offline and the approval_prompt parameter to force in the initial authorization request or your client configuration. - Kaspars Ozols
嘿@DrXCheng感谢您的帮助,因为我可以在您的代码的帮助下获得其他类型应用程序的访问令牌,但我无法获得web服务器应用程序和ios应用程序的accessToken和刷新令牌,所以,请你帮助我。如果我通过ios应用程序然后我没有客户端秘密,那么我如何获得访问令牌和刷新令牌? - Pankaj