本文介绍了谷歌oauth2 redirect_uri与几个参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将参数添加到google oauth2 redirect_uri?



就像这个 redirect_uri = http://www.example.com/ redirect.html?a = b 。



a = b 是随机的。

任何人都可以提供帮助吗?

解决方案

  1. 不能向重定向uri添加任何内容,在Oauth的应用程序设置中

    例如: p>


  2. 要传递几个参数给你的redirect uri,在调用之前将它们存储在 state
    参数中Oauth url,授权后的url会将相同的参数发送到您的redirect uri,如
    state = THE_STATE_PARAMETERS


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

/ 1。创建参数的json字符串 - >



{a:b,c:1}



/ 2。做一个base64UrlEncode,使其URL安全 - >

  stateString = base64UrlEncode('{a:b, c:1}'); 

这是一个base64UrlEncoding& amp; amp;解码():

 函数base64UrlEncode($ inputStr)
{
return strtr(base64_encode($ inputStr),'+ / =','-_,');


函数base64UrlDecode($ inputStr)
{
return base64_decode(strtr($ inputStr,'-_,','+ / ='));





$ b因此现在状态会是这样的:stateString - > asawerwerwfgsg,



在OAuth授权网址中传递此状态:

  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,

对于服务器端的流程,它会附带令牌:


对于客户端流程,它将随访问令牌一起发送到哈希:


How to add a parameters to the google oauth2 redirect_uri?

Just like this redirect_uri=http://www.example.com/redirect.html?a=b.

The b of a=b is random.

Anyone can help ?

解决方案
  1. You cannot add anything to the redirect uri, redirect uri is constant as setin the app settings of Oauth.eg :http://www.example.com/redirect.html

  2. To pass several parameters to your redirect uri, have them stored in stateparameter before calling Oauth url, the url after authorization will send the same parameters to your redirect uri asstate=THE_STATE_PARAMETERS

So for your case,do this:

/1. create a json string of your parameters ->

{ "a" : "b" , "c" : 1 }

/2. do a base64UrlEncode , to make it URL safe ->

stateString = base64UrlEncode('{ "a" : "b" , "c" : 1 }');

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, '-_,', '+/='));
}

So now state would be something like: stateString -> asawerwerwfgsg,

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,

For server side flow it will come along with token :http://www.example.com/redirect.html?token=sdfwerwqerqwer&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,

Retrieve the state, base64UrlDecode it, json_decode it, and you have your data.

See more about google OAuth 2 here:

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

这篇关于谷歌oauth2 redirect_uri与几个参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 16:48