【问题标题】:Youtube API single-user scenario with OAuth (uploading videos)使用 OAuth 的 Youtube API 单用户场景(上传视频)
【发布时间】:2011-08-16 13:21:12
【问题描述】:

这个问题已经被问过了,但从未回答过。

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

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

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

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

谢谢 米。

【问题讨论】:

    标签: oauth youtube-api access-token single-user


    【解决方案1】:

    为已安装的应用尝试 OAuth 2.0:http://code.google.com/apis/youtube/2.0/developers_guide_protocol.html#OAuth2_Installed_Applications_Flow

    首先,注册 API 以获取 client_id。

    然后,登录您的 google 帐户,输入以下 URL,将 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=code&access_type=offline

    然后你授权你自己的应用程序并获得一个授权码。

    然后打开一个终端并输入(更改您的代码、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。

    【讨论】:

    • 它就像一个魅力,但值得一提的是 1) 代码只能使用一次; 2) 代码仅在 10 分钟内有效(所以快点); 3)我必须设置额外的参数 approval_prompthereIf 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. 所述
    • 嘿 @DrXCheng 感谢您的帮助,因为我可以借助您的代码获取其他类型应用程序的访问令牌,但我无法获取 Web 服务器应用程序和 ios 的 accessToken 和刷新令牌应用程序,所以你能帮帮我吗?如果我通过 ios 应用程序,那么我没有客户端密码,那么如何获取访问令牌和刷新令牌?
    猜你喜欢
    • 2015-12-28
    • 2012-05-21
    • 1970-01-01
    • 1970-01-01
    • 2018-02-06
    • 2011-11-19
    • 2012-06-25
    • 2011-05-14
    • 2016-02-24
    相关资源
    最近更新 更多