Google Oauth-2.0返回带有哈希而不是问号的url



我使用此表单请求谷歌离线代币

    <form action="https://accounts.google.com/o/oauth2/auth" methode="POST">
        <input type="hidden" name="access_type" value="offline" />
        <input type="hidden" name="client_id" value="XXX" />
        <input type="hidden" name="scope" value="https://www.googleapis.com/auth/analytics https://www.googleapis.com/auth/analytics.edit https://www.googleapis.com/auth/analytics.manage.users https://www.googleapis.com/auth/analytics.manage.users.readonly https://www.googleapis.com/auth/analytics.readonly" />
        <input type="hidden" name="response_type" value="code token gsession" />
        <input type="hidden" name="redirect_uri" value="http://example.com/analytics" />
        <input type="hidden" name="approval_prompt" value="force" />
        <button>Get or Refresh token</button>
    </form>

为什么谷歌会给我回一个URL,比如:

http://example.com/analytics#access_token=XXX&token_type=Bearer&expires_in=3600&code=YYY&authuser=0&num_sessions=1&prompt=consent&session_state=ZZZ

请注意,我们在主url部分后面有一个哈希代码#,而不是问号?

我本来希望用PHP $_GET获得上面的变量,但我不能,因为那个散列代码。

我的问题是如何获得一个带有问号的url?

您使用的是code token gsessionresponse_type,它触发了一个所谓的Implicit流,其中access_token将在URL片段中返回。使用response_type=code获取作为查询参数的code值,您可以在令牌端点使用该值将其交换为access_token,如中所述:https://developers.google.com/accounts/docs/OAuth2WebServer#handlingtheresponse

最新更新