【问题标题】:how to authenticate google app script with GAE application using OAuth 2.0 [ GAS and GAE ]如何使用 OAuth 2.0 [ GAS 和 GAE ] 使用 GAE 应用程序验证谷歌应用程序脚本
【发布时间】:2016-04-07 18:31:56
【问题描述】:

我们已经开发了一个应用程序,它将使用 OAuth 1.0 从 GAS 脚本与 GAE 应用程序交互,但我们无法使用 OAuth 2.0 实现它,我们从 OAuth 获得访问失败。

getservice.hasAccess() 总是返回 false。我对必须使用哪个 URL 进行身份验证过程感到非常困惑。

我已附上 OAuth 1 和 OAuth 2 的源代码以供参考

Oauth 1.0
=========
envVars.url = myapp.appspot.com;

  var oAuthConfig = UrlFetchApp.addOAuthService(envVars.serviceName);
  oAuthConfig.setRequestTokenUrl(envVars.url + '/_ah/OAuthGetRequestToken');
  oAuthConfig.setAuthorizationUrl(envVars.url + '/_ah/OAuthAuthorizeToken');
  oAuthConfig.setAccessTokenUrl(envVars.url + '/_ah/OAuthGetAccessToken');

  var requestData = {
    "oAuthServiceName": envVars.serviceName,
    "oAuthUseToken": "always"
  };

  requestData.muteHttpExceptions = true;




Oauth 2.0
=========

envVars.url = myapp.appspot.com
 getservice = getService();


 if (getservice.hasAccess()) {
    Logger.log("All OK");
    return;
  }
 else {
    var authorizationUrl = getservice.getAuthorizationUrl();
    Logger.log('Open the following URL and re-run the script: %s',
               authorizationUrl);
    showURL(authorizationUrl);
  }

function getService()
{
   return OAuth2.createService(envVars.serviceName)
  // Set the endpoint URLs.
  .setAuthorizationBaseUrl(envVars.url + '/_ah/OAuthAuthorizeToken')
  .setTokenUrl(envVars.url + '/_ah/OAuthGetAccessToken')               


  // Set the client ID and secret.
  .setClientId(CLIENT_ID)
  .setClientSecret(CLIENT_SECRET)

  // Set the name of the callback function that should be invoked to complete
  // the OAuth flow.
  .setCallbackFunction('authCallback')

  // Set the property store where authorized tokens should be persisted.
  .setPropertyStore(PropertiesService.getUserProperties());
}

【问题讨论】:

    标签: google-app-engine google-apps-script oauth-2.0 google-oauth


    【解决方案1】:

    请注意,OAuth 1.0 已于 2012 年 4 月 20 日正式弃用。建议您尽快迁移 OAuth 1.0 to OAuth 2.0

    如果脚本使用可以访问私有数据的服务,您将遇到授权对话框。 Apps 脚本确定授权范围。

    如果代码更改添加了新服务,您之前授权的脚本也会要求额外授权。如果脚本仅使用无法访问用户数据的服务,则脚本不会请求授权

    查看讨论权限和脚本类型的文档:https://developers.google.com/apps-script/guides/services/authorization#permissions_and_types_of_scripts

    【讨论】:

    • 嗨,Datul,感谢您的回复,但我已经实现了 OAuth2 的代码,我在使用哪个 URL 进行 GAE 身份验证时遇到问题。
    猜你喜欢
    • 2017-06-06
    • 2011-09-18
    • 2011-06-07
    • 2012-12-13
    • 1970-01-01
    • 1970-01-01
    • 2023-02-11
    • 1970-01-01
    • 2015-08-14
    相关资源
    最近更新 更多