【问题标题】:Does Google AppScript (GAS) supports O-Auth 2.0 Implicit Grant-TypeGoogle App Script (GAS) 是否支持 OAuth 2.0 隐式授权类型
【发布时间】:2019-03-11 07:24:54
【问题描述】:

我正在尝试使用 Google Apps 脚本创建新的 Gmail 插件并尝试访问第三方非 Google API。为此,我使用 O-Auth 2.0 Implicit Grant-Type 进行身份验证。

这就是AuthService 的样子:

function getOAuthService() {
  return OAuth2.createService('Podio O-Auth')
    .setAuthorizationBaseUrl('Base Url')
    .setTokenUrl('Token Url')
    .setClientId('clientId')
    .setClientSecret('clientSecret')
    .setParam('redirect_uri', 'https://script.google.com/macros/d/' + scriptID + '/usercallback')
    .setScope('GLOBAL')
    .setCallbackFunction('authCallback')
    .setCache(CacheService.getUserCache())
    .setParam('response_type', 'token')
    .setParam('response_mode', 'query')
    .setParam('state', getStateToken('authCallback')) // function to generate the state token on the fly
    .setPropertyStore(PropertiesService.getUserProperties());
}

脚本正确生成了一个包含我的redirect_uri的 URL
Auth 获取请求,生成令牌,并将我重定向到 scripts.google.com 域。

点击scripts.google.com 后,我会被重定向到包含我的自定义域的 URL,例如

https://script.google.com/a/macros/[custom-domain]/d/[script-id]/usercallback#access_token=[token]&expires_in=7200&token_type=Bearer&state=[state]&id_token=[token]

导致此错误的原因:

因为 url 被# 分割。如果我将# 替换为?,那么它会按预期工作。

谁能告诉我如何解决这个问题?如果没有,那么我是否必须为此目的授权代码授予流程?

注意:我已将 setParam('response_type', 'token') 用于隐式授予类型

【问题讨论】:

  • 如果您向您正在使用的OAuth2 库的作者咨询,您可能会获得一些额外的信息。有人会认为,如果 Apps Script 不支持 Implicit Grant,那么为 Apps Script 编写的 OAuth2 库同样不会启用 Implicit Grant。
  • @tehhowch OP 确实并且已经收到了作者的答复。他可能正在等待其他答案。

标签: authentication google-apps-script oauth-2.0 google-oauth gmail-addons


【解决方案1】:

该库目前不支持隐式授权。 Google AppScript 支持服务器端流程。 所以,我设置了response_type = code,这是工作授权服务的样子:

function getOAuthService() {
  return OAuth2.createService('Podio O-Auth')
    .setAuthorizationBaseUrl('Base Url')
    .setTokenUrl('Token Url')
    .setClientId('clientId')
    .setClientSecret('clientSecret')
    .setParam('redirect_uri', 'https://script.google.com/macros/d/' + scriptID + '/usercallback')
    .setScope('GLOBAL')
    .setCallbackFunction('authCallback')
    .setCache(CacheService.getUserCache())
    .setParam('response_type', 'code')
    .setParam('response_mode', 'query')
    .setParam('state', getStateToken('authCallback')) // function to generate the state token on the fly
    .setPropertyStore(PropertiesService.getUserProperties());
}

它在内部首先调用 autorizatiionBaseUrl 并接收授权码。并使用此授权代码再次向 TokenUrl 发出 post 请求以获取 auth_token、refresh_token 和其他详细信息。 谢谢。 :)

【讨论】:

    【解决方案2】:

    根据您在 apps-script-oauth2GitHub repo 中的问题,您在 Apps 脚本中的特定 OAuth 实现(使用该库)不支持隐式授权。鉴于 Apps 脚本在服务器(而不是客户端,隐式授权最有用)中执行,您使用的库也不太可能被扩展以支持它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-08-27
      • 1970-01-01
      • 1970-01-01
      • 2018-05-18
      • 2019-02-11
      • 1970-01-01
      • 2013-03-10
      • 2012-12-27
      相关资源
      最近更新 更多