【问题标题】:Google cannot redirect to the URL I set, OAuth 2Google 无法重定向到我设置的 URL,OAuth 2
【发布时间】:2012-10-06 03:41:27
【问题描述】:

我正在尝试使用 OAuth 2.0 登录 Google。在我获得临时授权码并使用它获取access_token 后,Google 不会将我重定向到我在发布时设置的 URL,这是我发布的代码:

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

在我获得 JSON 中的 access_token 后,它不会将我重定向到我的 URL,而是停留在页面“https://accounts.google.com/o/oauth2/token”中,我不知道为什么。我做错了吗?

【问题讨论】:

    标签: php redirect oauth-2.0 youtube-api


    【解决方案1】:

    exchange authorization code for an access token step 中不涉及重定向。您应该在这里使用服务器端 POST 请求,您可以在其中获取 access tokenrefresh token 作为直接响应。在您的示例中,用户的浏览器会发送这样的请求,但不会监听其响应。

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

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

    【讨论】:

      【解决方案2】:

      如果您想在服务器端执行此操作,Jan 的响应是正确的。

      如果您想在浏览器中处理客户端的事情,可以使用单独的 OAuth 2 for Web Applications 流程:

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

      适用于 JavaScript 的 Google APIs 客户端库可以很容易地实现这一点:

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

      还有一个您可以使用/借用代码的现场示例:

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

      【讨论】:

      • 是的,正确 ;) 由于问题被标记为 php 我只是指更安全的服务器端流程,但哪个流程更适合显然取决于关于具体用例。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-05
      • 1970-01-01
      • 1970-01-01
      • 2015-08-28
      • 1970-01-01
      相关资源
      最近更新 更多