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

问题描述

如何向 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=bb 是随机的.

有人可以帮忙吗?

推荐答案

  1. 您不能向重定向 uri 添加任何内容,重定向 uri 是固定不变的在 Oauth 的应用程序设置中.例如:http://www.example.com/redirect.html

要将多个参数传递给重定向 uri,请将它们存储在 state调用 Oauth url 之前的参数,授权后的 url 将向您的重定向 uri 发送相同的参数作为state=THE_STATE_PARAMETERS

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.创建参数的 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, '-_,', '+/='));
}

所以现在 state 应该是这样的: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,

检索状态,base64UrlDecode,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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-20 16:50