如何向Google OAuth 2.0 redirect_uri
添加参数?
像这样:
b
的a=b
是随机的。
有人可以帮忙吗?
最佳答案
在Oauth的应用设置中。
例如:http://www.example.com/redirect.html
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