php
  • redirect
  • oauth-2.0
  • youtube-api
  • 2012-10-05 92 views 0 likes 
    0

    我尝试使用OAuth 2.0登录Google。之后我得到了临时授权码,然后用它来获取access_token,谷歌不会重定向我我设置的URL时后,这里是我的代码做专:
    Google无法重定向到我设置的网址,OAuth 2

    <form action="https://accounts.google.com/o/oauth2/token" method="post"> 
    <input type="text" name="code" value='##code##'/> 
    <input type="text" name="client_id" value='##client_id##'/> 
    <input type="text" name="client_secret" value='##client_secret##'/> 
    <input type="text" name="redirect_uri" value='http://boomroom.tv/test.php'/> 
    <input type="text" name="grant_type" value='authorization_code'/> 
    <input type="submit" /></form> 
    

    我得到了后access_token在JSON中,它不会将我重定向到我的网址,但保留在页面'https://accounts.google.com/o/oauth2/token'中,我不知道为什么。难道我做错了什么?

    回答

    2

    exchange authorization code for an access token step中没有涉及重定向。你应该在这里使用服务器端的POST请求,在这里你可以获得访问令牌刷新令牌作为直接响应。在你的例子中,用户的浏览器会发送这样的请求,但不听其响应。

    请注意,您的客户端机密可以在您网站的源代码中公开访问。

    有多种方法可以使用PHP执行服务器端POST请求,例如, using cURL

    1

    如果你想做这个服务器端,Jan的回答是正确的。

    如果你要处理的事情在浏览器客户端,有一个单独的OAuth 2 Web应用程序的流量,你可以使用:

    https://developers.google.com/youtube/2.0/developers_guide_protocol_oauth2#OAuth2_Client_Side_Web_Applications_Flow

    的谷歌API客户端JavaScript库使得它非常容易实现这一点:

    https://code.google.com/p/google-api-javascript-client/wiki/Authentication

    还有就是你可以用/借用代码在播放实时样本:

    http://gdata-samples.googlecode.com/svn/trunk/gdata/youtube_upload_cors.html

    +0

    是的,正确的;)作为问题被标记为* PHP *我只是指的服务器端的流动,这是更安全,但其流量明显好转适合取决于具体的使用情况。 –

    相关问题