繁体   English   中英

将Google Apps脚本从OAuth 1.0 OAuthConfig API和UrlFetchApp.addOAuthService迁移到OAuth 2.0

[英]Migrating Google Apps Scripts from OAuth 1.0 OAuthConfig API and UrlFetchApp.addOAuthService to OAuth 2.0

我有几个重要的Google Apps脚本,它们在Google表格电子表格中是“容器绑定的”。 他们调用Google Admin SDKDirectory API和其他一些Google API,例如URL Shortener API

我最初是通过谷歌搜索并在Stack Overflow上搜索来完成这些工作的,直到找到满足我需要的代码。

他们现在可以正常工作,但是在过去的一个月中,我收到了Google的电子邮件,其中已弃用OAuth 1.0,并且使用它的所有内容都应在2015年4月20日之前迁移到OAuth 2.0。

我阅读了他们的官方文档和迁移指南,但仍然感到非常困惑。 他们的所有文档似乎都是针对“应用程序”的,我找不到Google Apps脚本和Javascript特有的内容。

这是我拥有的有效Google Apps脚本的示例。 它以电子邮件地址作为输入,并返回用户的全名。

var consumerKey = "domain.com";
var consumerSecret = "xxxxxxxxxxxxxxxxxxxxxxxx";
var publicApiAccessKey = 'xxxxxxxxxxxxx_xxxxxxxxxxxxxxxxxxxxxxxxx';

function getUsersName(email) {

  var name = '';

  try {

    var url = 'https://www.googleapis.com/admin/directory/v1/users/' + email + '?key=' + publicApiAccessKey;
    var scope = "https://www.googleapis.com/auth/admin.directory.user.readonly";

    var fetchArgs = googleOAuth_("Users",scope);
    fetchArgs.method = "GET";
    fetchArgs.muteHttpExceptions=true;

    var userJson = UrlFetchApp.fetch(url, fetchArgs);

    var userObject = JSON.parse(userJson);

    name = userObject.name.fullName;

  } catch(e) {

    // send failure email

  }

  return name;

}

function googleOAuth_(name,scope) {

  var oAuthConfig = UrlFetchApp.addOAuthService(name);
  oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken?scope=" + scope);
  oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
  oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
  oAuthConfig.setConsumerKey(consumerKey);
  oAuthConfig.setConsumerSecret(consumerSecret);
  return {oAuthServiceName:name, oAuthUseToken:'always'};

}

当我运行它时,我得到以下警告提示,确认我的Google Apps脚本使用的代码已被弃用,应予以替换:

执行提示

以下是Google发送的电子邮件文本,用于通知客户已弃用的服务:

提醒ClientLogin,OAuth 1.0,AuthSub和OpenID 2.0关闭Hello Administrator,

在过去的几年中,我们宣布不推荐使用ClientLogin,OAuth 1.0(3LO),AuthSub和OpenID 2.0,并将于2015年4月20日关闭。Google将不再使用这些较旧的协议,以便将支持重点放在最新的Internet标准,OAuth 2.0和OpenID Connect。

对我们的日志的分析使我们相信您的domain domain.com运行的一个或多个应用程序仍然使用这些不赞成使用的协议之一。 您需要确保您的应用程序使用我们支持的身份验证方法:用于API授权的OAuth 2.0或用于联合身份验证的OpenID Connect。 迁移到这些新标准的最简单方法是使用Google登录SDK(请参阅迁移文档)。 Google登录建立在我们的OAuth 2.0和OpenID Connect基础架构之上,并为Web,Android和iOS上的身份验证和授权流程提供了一个单一的界面。

如果这些应用程序的迁移未在截止日期之前完成,则该应用程序将无法连接到Google(可能包括登录功能),直到迁移到支持的身份验证协议为止。 为避免服务中断,至关重要的是您必须在关闭日期之前进行迁移。

如果您需要迁移与Google的集成,请执行以下操作:

从OpenID 2.0迁移到Google登录(OpenID Connect)。 OAuth 1.0迁移到OAuth 2.0 对于AuthSub和ClientLogin,没有迁移支持。 您需要从OAuth 2.0重新开始,并且用户需要重新同意。 如果您有关于迁移应用程序的任何技术问题,请使用google-oauth或google-openid标签将问题发布到Stack Overflow。 请记住,堆栈溢出是一个公开可见的论坛。 任何发布的问题都会定期通知我们的工程师。

我阅读了从OAuth 1.0迁移到OAuth 2.0一节,但是我仍然不知道我应该做什么。 我需要特别了解我需要在上面的代码中进行哪些更改,以便脚本在4月20日(下周一)之后继续工作。

转换需要两个步骤:

1)您需要向脚本添加Oauth2 Webflow。 您可以通过以下网址获取Google开发的库: https//github.com/googlesamples/apps-script-oauth2

查看此存储库中的README.MD,以了解如何进行设置。

2)更改呼叫以使用Oauth2令牌:

function getUsersName(email) {
var token = oauth2Service().getAccessToken(); //this will be setup from step 1
  var name = '';

  try {

    var url = 'https://www.googleapis.com/admin/directory/v1/users/' + email;


   var parameters = { method : 'get',
                headers : {'Authorization': 'Bearer '+ token},
                contentType:'application/json',                    
                muteHttpExceptions:true};

    var userJson = UrlFetchApp.fetch(url, parameters);

    var userObject = JSON.parse(userJson);

    name = userObject.name.fullName;

  } catch(e) {

    // send failure email

  }

  return name;

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM