带有多个参数的Google OAuth 2.0 redirect_uri [英] Google OAuth 2.0 redirect_uri with several parameters
问题描述
如何向Google OAuth 2.0 redirect_uri
添加参数?
How to add a parameters to the Google OAuth 2.0 redirect_uri
?
就像这样:
redirect_uri=http://www.example.com/redirect.html?a=b
a=b
的b
是随机的.
任何人都可以帮忙吗?
推荐答案
-
您无法向重定向uri添加任何内容,重定向uri设置为常数 在Oauth的应用设置中. 例如: http://www.example.com/redirect.html
You cannot add anything to the redirect uri, redirect uri is constant as set in the app settings of Oauth. eg :http://www.example.com/redirect.html
要将多个参数传递到重定向uri,请将它们存储在state
中
参数,然后调用Oauth网址,授权后的网址将向您的重定向uri发送相同的参数,如下所示:
state=THE_STATE_PARAMETERS
To pass several parameters to your redirect uri, have them stored in state
parameter before calling Oauth url, the url after authorization will send the same parameters to your redirect uri as
state=THE_STATE_PARAMETERS
对于您的情况,请执行以下操作:
So for your case,do this:
/1.创建一个参数的json字符串->
/1. create a json string of your parameters ->
{ "a" : "b" , "c" : 1 }
/2.进行base64UrlEncode,以使其URL安全->
/2. do a base64UrlEncode , to make it URL safe ->
stateString = base64UrlEncode('{ "a" : "b" , "c" : 1 }');
这是base64UrlEncoding&的PHP示例.解码( http://en.wikipedia.org/wiki/Base64#URL_applications ):
This is a PHP example of base64UrlEncoding & decoding (http://en.wikipedia.org/wiki/Base64#URL_applications) :
function base64UrlEncode($inputStr)
{
return strtr(base64_encode($inputStr), '+/=', '-_,');
}
function base64UrlDecode($inputStr)
{
return base64_decode(strtr($inputStr, '-_,', '+/='));
}
现在状态应该是这样:stateString-> asawerwerwfgsg,
So now state would be something like: stateString -> asawerwerwfgsg,
在OAuth授权URL中传递此状态:
Pass this state in OAuth authorization 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,
对于服务器端流,它将附带令牌: http://www.example.com/redirect.html?token=sdfwerwqerqwer& ; state = asdafwswdwefwsdg ,
For server side flow it will come along with token : http://www.example.com/redirect.html?token=sdfwerwqerqwer&state=asdafwswdwefwsdg,
对于客户端流,它将与访问令牌一起放入哈希中: http://www.example.com/redirect.html#access_token=portyefghsdfgdfgsdgd& ; state = asdafwswdwefwsdg ,
For client side flow it will come in the hash along with access token: http://www.example.com/redirect.html#access_token=portyefghsdfgdfgsdgd&state=asdafwswdwefwsdg,
检索状态,通过base64Url对其进行解码,对其进行json_decode即可得到数据.
Retrieve the state, base64UrlDecode it, json_decode it, and you have your data.
在此处查看有关google OAuth 2的更多信息:
See more about google OAuth 2 here:
http://code.google.com/apis/accounts/docs/OAuth2.html
这篇关于带有多个参数的Google OAuth 2.0 redirect_uri的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!