【问题标题】:Multiple Scope Values to oauth2oauth2 的多个范围值
【发布时间】:2012-01-16 23:38:02
【问题描述】:

我尝试发布几个范围值以允许我的应用程序用于某些 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" />  

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

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>

【问题讨论】:

    标签: oauth google-api oauth-2.0


    【解决方案1】:

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

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

    【讨论】:

    • 我直到明天才有我的代码,但如果它看起来很奇怪,谢谢你埃文
    • 目前在2017年你应该使用value="https://www.googleapis.com/auth/calendar email"
    • 为了好奇,RFC 6749, Section 3.3scope 参数定义为The value of the scope parameter is expressed as a list of space-delimited, case-sensitive strings
    【解决方案2】:

    除了 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"
    

    我在文档中没有找到任何相关信息。

    【讨论】:

      【解决方案3】:

      为了清楚起见,您可以将所有范围放在 1 个数组中:

       const scopes = [
          'openid',
          'https://www.googleapis.com/auth/userinfo.profile',
          'https://www.googleapis.com/auth/userinfo.email',
          'https://www.googleapis.com/auth/gmail.readonly',
        ]
      
        const scope = scopes.join(' ')
      
        console.log(scope)
        // openid https://www.googleapis.com/auth/userinfo.profile https://www.googleapis.com/auth/userinfo.email https://www.googleapis.com/auth/gmail.readonly
      
      
        const redirectUri = 'http://localhost:3000'
        const link = `https://accounts.google.com/o/oauth2/v2/auth?access_type=offline&scope=${scope}&response_type=code&client_id=${GOOGLE.clientId}&redirect_uri=${redirectUri}&state=authGoogle`
      

      【讨论】:

        猜你喜欢
        • 2020-05-12
        • 1970-01-01
        • 2021-02-24
        • 1970-01-01
        • 2021-12-04
        • 1970-01-01
        • 2012-07-14
        • 2013-03-22
        • 1970-01-01
        相关资源
        最近更新 更多