首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >谷歌OAuth 2.0 include_granted_scopes不适用于已安装的应用程序

谷歌OAuth 2.0 include_granted_scopes不适用于已安装的应用程序
EN

Stack Overflow用户
提问于 2014-02-24 13:30:01
回答 2查看 4.6K关注 0票数 9

我试图为已安装的应用程序使用新的增量授权,以便在保留现有作用域的同时将作用域添加到现有授权中。这是使用新的include_granted_scopes=true参数完成的。然而,无论我尝试了什么,重新授权总是完全覆盖范围。下面是我为演示我的问题而编写的一个最小的Bash PoC脚本:

代码语言:javascript
运行
复制
client_id='716905662885.apps.googleusercontent.com' # throw away client_id (non-prod)
client_secret='CMVqIy_iQqBEMlzjYffdYM8A' # not really a secret
redirect_uri='urn:ietf:wg:oauth:2.0:oob'

while :
do
  echo "Please enter a list of scopes (space separated) or CTRL+C to quit:"
  read scope

  # Form the request URL
  # http://goo.gl/U0uKEb
  auth_url="https://accounts.google.com/o/oauth2/auth?scope=$scope&redirect_uri=$redirect_uri&response_type=code&client_id=$client_id&approval_prompt=force&include_granted_scopes=true"

  echo "Please go to:"
  echo
  echo "$auth_url"
  echo
  echo "after accepting, enter the code you are given:"
  read auth_code

  # swap authorization code for access token
  # http://goo.gl/Mu9E5J
  auth_result=$(curl -s https://accounts.google.com/o/oauth2/token \
    -H "Content-Type: application/x-www-form-urlencoded" \
    -d code=$auth_code \
    -d client_id=$client_id \
    -d client_secret=$client_secret \
    -d redirect_uri=$redirect_uri \
    -d grant_type=authorization_code)
  access_token=$(echo -e "$auth_result" | \
               grep -Po '"access_token" *: *.*?[^\\]",' | \
               awk -F'"' '{ print $4 }')

  echo
  echo "Got an access token of:"
  echo $access_token
  echo

  # Show information about our access token
  info_result=$(curl -s --get https://www.googleapis.com/oauth2/v2/tokeninfo \
    -H "Content-Type: application/json" \
    -d access_token=$access_token)
  current_scopes=$(echo -e "$info_result" | \
                   grep -Po '"scope" *: *.*?[^\\]",' | \
                   awk -F'"' '{ print $4 }')

  echo "Our access token now allows the following scopes:"
  echo $current_scopes | tr " " "\n"
  echo
  echo "Let's add some more!"
  echo

done

脚本只需执行OAuth授权,然后打印令牌当前授权使用的范围。理论上,每次都应该继续添加作用域,但实际上,每次都会覆盖作用域列表。因此,在第一次运行时,您可以使用email之类的最小范围,然后在下一次运行时使用更类似于只读日历https://www.googleapis.com/auth/calendar.readonly的内容。每次,应该只提示用户授权当前请求的作用域,但是得到的令牌应该对所有作用域都有好处,包括以前运行时授权的范围。

我尝试了一个新的client_id/secret,结果是一样的。我知道我可以再次包含已经授权的作用域,但这会提示用户输入所有作用域,甚至那些已经授予的作用域,而且我们都知道范围列表越长,用户接受的可能性就越小。

更新:在进一步测试期间,我注意到确实显示了每个增量授权的组合范围。我试着在增量auth之后等待30秒左右,然后使用刷新令牌获取一个新的访问令牌,但是该访问令牌仍然仅限于上一次授权的作用域,而不是组合的范围列表。

更新2:我还玩过保留原始刷新令牌的。刷新令牌只获得允许原始作用域的新访问令牌,不包括增量添加的作用域。因此,实际上,include_granted_scopes=true对令牌没有任何影响,旧的和新的刷新令牌继续工作,但只对它们指定的范围起作用。我无法获得“组合作用域”刷新或访问令牌。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-01-16 20:43:20

谷歌的OAuth 2.0服务不支持安装/本地应用程序的增量服务;它只适用于web服务器案例。他们的文件坏了。

票数 9
EN

Stack Overflow用户

发布于 2014-03-12 12:31:40

尝试向第二个请求添加一个完整的作用域列表,在该请求中,您可以将授权代码交换为访问令牌。奇怪的是,scope参数似乎没有被记录在案,但它存在于由生成的请求中。例如:

代码语言:javascript
运行
复制
code=foo&grant_type=authorization_code
&redirect_uri=http%3A%2F%2Flocalhost%3A8080%2Fmyapp%2FoauthCallback
&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.profile+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.me+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fplus.stream.write

web服务器场景中,当include_granted_scopes设置为true时,将返回授予作用域的完整列表和授权代码。这是链接文档中似乎缺少的另一小部分信息。

在我们的应用程序中,包含代码交换请求中的作用域的完整列表的编辑1适用于我们,但是我刚刚尝试了您的原始脚本,没有进行任何修改(除了客户机id/),它运行得很好(只编辑了id和令牌):

代码语言:javascript
运行
复制
$ bash tokens.sh
Please enter a list of scopes (space separated) or CTRL+C to quit:
https://www.googleapis.com/auth/userinfo.profile
Please go to:

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/userinfo.profile&redirect_uri=urn:ietf:wg:oauth:2.0:oob&response_type=code&client_id=189044568151-4bs2mcotfi2i3k6qp7vq8c6kbmkp2rf8.apps.googleusercontent.com&approval_prompt=force&include_granted_scopes=true

after accepting, enter the code you are given:
4/4qXGQ6Pt5QNYqdEuOudzY5G0ogru.kv_pt5Hlwq8UYKs_1NgQtlUFsAJ_iQI

Got an access token of:
ya29.1.AADtN_XIt8uUZ_zGZEZk7l9KuNQl9omr2FRXYAqf67QF92KqfvXliYQ54ffg_3E

Our access token now allows the following scopes:
https://www.googleapis.com/auth/userinfo.profile
https://www.googleapis.com/auth/userinfo.email
https://www.googleapis.com/auth/plus.me
https://www.googleapis.com/auth/plus.circles.read

您可以看到以前授予的作用域包括在.

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21989255

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档