繁体   English   中英

授权Google Apps脚本访问云端硬盘文件

[英]Authorizing Google Apps Script to access Drive files

以下代码可以很好地获取我自己的Google+用户属性:

// Google oAuth
function getAuth(service, scopes) {
   var oAuthConfig = UrlFetchApp.addOAuthService(service);
   oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/OAuthGetRequestToken? scope="+scopes);
   oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
   oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
   oAuthConfig.setConsumerKey("anonymous");
   oAuthConfig.setConsumerSecret("anonymous");
   return {oAuthServiceName:service, oAuthUseToken:"always"};
 }

 function test_API_Key() {
    var gPlusAuth = getAuth("plus", "https://www.googleapis.com/auth/plus.me");
    var url = "https://www.googleapis.com/plus/v1/people/me?key=" + API_KEY;
    var httpResponse = UrlFetchApp.fetch(url, gPlusAuth);
    Logger.log(httpResponse);
  }

当我使用相同的方法访问有关Google云端硬盘中文件的元数据时,遇到HTTP错误403和“未配置访问权限”。 下面是代码。 我究竟做错了什么?

function getFileMetaData(fileId) {
  var driveAuth = getAuth("drive", "https://www.googleapis.com/auth/drive");
  var url = "https://www.googleapis.com/drive/v2/files/" + 
        documentID + "?key=" + API_KEY;
  var response = UrlFetchApp.fetch(url, driveAuth);
  Logger.log(response);
}

取自Google开发人员网站[1]

在较高级别上,所有应用程序都遵循相同的基本授权模式:

  • 在Google Developers Console中注册该应用程序。
  • 要求用户授予对其Google帐户中数据的访问权限。
  • 如果用户同意,您的应用程序将请求并接收访问Drive API的凭据。
  • 刷新凭据(如有必要)。
  <script type="text/javascript">
    var CLIENT_ID = '<YOUR_CLIENT_ID>';
    var SCOPES = [
      'https://www.googleapis.com/auth/drive.file',
      'https://www.googleapis.com/auth/userinfo.email',
      'https://www.googleapis.com/auth/userinfo.profile',
      // Add other scopes needed by your application.
    ];

[1] https://developers.google.com/drive/web/about-auth

暂无
暂无

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

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