ChatGPT解决这个技术问题 Extra ChatGPT

oauth2 的多个范围值

我尝试发布几个范围值以允许我的应用程序用于某些谷歌服务......

我尝试了两个输入字段

<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>

P
Pang

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

<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
F
Filip Kowal

除了史蒂夫巴兹尔的回答。当为同一个 Google 服务应用多个范围时,范围的顺序似乎很重要。 Fe 这个字符串按预期工作:

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

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


A
Alan

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

关注公众号,不定期副业成功案例分享
关注公众号

不定期副业成功案例分享

领先一步获取最新的外包任务吗?

立即订阅