如何向Google OAuth 2.0 redirect_uri添加参数?
像这样:
ba=b是随机的。
有人可以帮忙吗?

最佳答案

  • 您无法向重定向uri添加任何内容,重定向uri设置为常量
    在Oauth的应用设置中。
    例如:http://www.example.com/redirect.html
  • 要将多个参数传递到重定向uri,请将它们存储在state
    参数,然后调用Oauth网址,授权后的网址将向您的重定向uri发送相同的参数,如下所示:state=THE_STATE_PARAMETERS

  • 因此,对于您的情况,请执行以下操作:

    / 1。创建参数的json字符串->
    { "a" : "b" , "c" : 1 }
    / 2。做一个base64UrlEncode,以使其URL安全->
    stateString = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
    

    这是base64UrlEncoding和解码(http://en.wikipedia.org/wiki/Base64#URL_applications)的PHP示例:
    function base64UrlEncode($inputStr)
    {
        return strtr(base64_encode($inputStr), '+/=', '-_,');
    }
    
    function base64UrlDecode($inputStr)
    {
        return base64_decode(strtr($inputStr, '-_,', '+/='));
    }
    

    所以现在状态将类似于:stateString-> asawerwerwfgsg,

    在OAuth授权URL中传递此状态:
    https://accounts.google.com/o/oauth2/auth?
      client_id=21302922996.apps.googleusercontent.com&
      redirect_uri=https://www.example.com/back&
      scope=https://www.google.com/m8/feeds/&
      response_type=token&
      state=asdafwswdwefwsdg,
    

    对于服务器端流,它将附带 token :
    http://www.example.com/redirect.html?token=sdfwerwqerqwer&state=asdafwswdwefwsdg

    对于客户端流,它将与访问 token 一起放入哈希中:
    http://www.example.com/redirect.html#access_token=portyefghsdfgdfgsdgd&state=asdafwswdwefwsdg

    检索状态,对它进行base64UrlDecode对其进行解码,对它进行json_decode进行处理,就可以得到您的数据。

    在此处查看有关google OAuth 2的更多信息:

    http://code.google.com/apis/accounts/docs/OAuth2.html

    10-07 20:04
    查看更多