oauth2的多个范围值

时间:2011-12-09 17:42:55

标签: oauth google-api oauth-2.0

我尝试发布sereval范围值以允许我的应用程序使用某些Google服务...

我尝试了两个输入字段

<input type="hidden" name="scope" value="https://www.googleapis.com/auth/calendar" />  
<input type="hidden" name="scope" value="https://www.googleapis.com/auth/userinfo.email" />

并带有一个带+分隔符的输入字段

<input type="hidden" name="scope" value="https://www.googleapis.com/auth/calendar+https://www.googleapis.com/auth/userinfo.email" />  

当我发送只有一个范围的表单时它可以工作。 否则使用sereval范围值谷歌重定向我与此错误描述:

http://localhost:49972/redirect.aspx#error=invalid_request&error_description=OAuth+2+parameters+can+only+have+a+single+value:+scope&error_uri=http://code.google.com/apis/accounts/docs/OAuth2.html 

在带有oAuth2的Google getting started中,它适用于两个范围值。

这是我的代码:

  <form id="form1" method="post" action="https://accounts.google.com/o/oauth2/auth?" >
    <div>
        <input type="hidden" name="response_type" value="code" />
        <input type="hidden" name="client_id" value="my client id" />
        <input type="hidden" name="redirect_uri" value="http://localhost:49972/redirect.aspx" />
        <input type="hidden" name="scope" value="https://www.googleapis.com/auth/calendar" />
        <input type="hidden" name="scope" value="https://www.googleapis.com/auth/userinfo.email" />

        <input type="hidden" name="state" value="/profile" />
        <input type="submit" value="go" />
    </div>
    </form>

2 个答案:

答案 0 :(得分:94)

当您将它们组合到单个字段时,您处于正确的轨道上 。请求中应该只有一个范围参数,值以空格分隔。如果你把它放在这样的形式,浏览器将负责为你编码空间。

<input type="hidden" name="scope" value="https://www.googleapis.com/auth/calendar https://www.googleapis.com/auth/userinfo.email" />

答案 1 :(得分:1)

除了史蒂夫·巴泽尔(Steve Bazyl)的答案。当对同一Google服务应用多个范围时,范围的顺序似乎很重要。 F.e该字符串按预期工作:

"https://www.googleapis.com/auth/drive https://www.googleapis.com/auth/drive.metadata.readonly"

这对我不起作用:

"https://www.googleapis.com/auth/drive.metadata.readonly https://www.googleapis.com/auth/drive"

虽然我在文档中没有找到有关此信息的任何信息。