oauth2 的多个范围值

问题描述 投票:0回答:4
oauth google-api oauth-2.0
4个回答
126
投票

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

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

4
投票

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

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


1
投票

为了清晰起见,您可以将所有范围放入 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`

0
投票

对于在omniauth文件中设置正确范围属性有困难的任何人,请尝试在nunosilva的该线程建议的omniauth路径/url中设置范围字符串 https://github.com/zquestz/omniauth-google-oauth2/issues/143

© www.soinside.com 2019 - 2024. All rights reserved.