我正在尝试使用新的 incremental authorization对于已安装的应用程序,以便在保留现有范围的同时向现有授权添加范围。这是使用新的 include_granted_scopes=true
参数完成的。但是,无论我尝试过什么,重新授权总是会完全覆盖范围。这是我为演示我的问题而编写的最小 Bash PoC 脚本:
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 授权,然后打印出 token 当前授权使用的范围。从理论上讲,它应该每次都继续添加范围,但实际上,范围列表每次都会被覆盖。所以这个想法是在第一次运行时,你会使用像 email
这样的最小范围,然后在下一次运行时,添加更像只读日历的东西 https://www.googleapis.com/auth/calendar.readonly
。每次,应该只提示用户授权当前请求的范围,但生成的 token 应该适用于所有范围,包括那些在之前运行时授权的范围。
我尝试使用新的 client_id/secret,结果是一样的。我知道我可以再次包含已经授权的范围,但这会提示用户输入所有范围,即使是那些已经授权的范围,我们都知道范围列表越长,用户接受的可能性就越小。
更新:在进一步测试期间,我注意到 permissions for my app确实显示每个增量授权的组合范围。我尝试在增量身份验证后等待 30 秒左右,然后使用刷新 token 获取新的访问 token ,但该访问 token 仍然限于上次授权的范围,而不是组合范围列表。
更新 2: 我也考虑过保留原始刷新 token 。刷新 token 仅获取允许原始范围的新访问 token ,不包括增量添加的范围。因此,include_granted_scopes=true
似乎对 token 没有影响,旧的和新的刷新 token 继续工作但仅适用于它们指定的范围。我无法获得“组合范围”刷新或访问 token 。
最佳答案
Google 的 OAuth 2.0 服务不支持已安装/ native 应用程序的增量身份验证;它只适用于 web server case .他们的文档已损坏。
关于bash - Google OAuth 2.0 include_granted_scopes 不适用于已安装的应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21989255/