0

下面是取自 Arun Nagarajan 示例的代码:我尝试使用相同的代码进行检查。但它没有正确安装。(我在下面删除了我的重定向网址、客户端 ID 和密码)。请告诉我下面的代码有什么问题。

var AUTHORIZE_URL = 'https://accounts.google.com/o/oauth2/auth';
var TOKEN_URL = 'https://accounts.google.com/o/oauth2/token';
var REDIRECT_URL =             'exec';
var tokenPropertyName = 'GOOGLE_OAUTH_TOKEN';

var CLIENT_ID = '';
var CLIENT_SECRET = '';

function doGet(e) {
var HTMLToOutput;

if(e.parameters.state){
var state = JSON.parse(e.parameters.state);
if(state.action === 'çreate'){
var meetingURL = createMeetingNotes();
HTMLToOutput = '<html><h1>Meeting notes document created!</h1><a href="'+meetingURL+'">    <click here to open</a></html>';
}
else if (state.ids){
var doc = DocsList.getFileById(state.ids[0]);
var url = doc.getContentAsString();
HTMLToOutput = '"<html><a href="' +url+'"</a></html>"';
}
else {
zipAndSend(state.ecportIds.Session.getEffectUser().getEmail());
HTMLToOutput = '"<html><h1>Email sent. Check your Inbox.</h1></html>"';
}
}
else if(e.parameters.code){
getAndStoreAccessToken(e.parameters.code);
HTMLToOutput = '<html><h1>App is installed. You can close this window now or navigate to your </h1><a href="https://drive.google.com/">Google Drive</a></html>';
}
else {
HTMLToOutput = '<html><h1>Install this App into your google drive </h1><a href="' + getURLForAuthorization() + '">Click here to start install</a></html>';
}

return HtmlService.createHtmlOutput(HTMLToOutput);

}

function getURLForAuthorization() {
return  AUTHORIZE_URL + '?response_type=code&client_id=' + CLIENT_ID + '&redirect_uri='     + REDIRECT_URL + '&scope=https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fdrive.install+https%3A%2F%2Fwww.googleapis.com%2Fauth%2Fuserinfo.email';
}

function getAndStoreAccessToken(code) {
var parameters = { method : 'post',
                  payload : 'client_id='+ CLIENT_ID + '&client_secret=' + CLIENT_SECRET + '&grant_type=authorization.code&redirect_uri=' + REDIRECT_URL};
var response = UrlFetchApp.fetch(TOKEN_URL.parameters).getContentText();
var tokenResponse = JSON.parse(response);
UserProperties.getProperty(tokenPropertyName, tokenResponse.access_token);
}

function getUrlFetchOptions() {
return {'contentType' : 'application/json',
        'headers' : {'Authorization': 'Bearer ' +         UserProperties.getProperty(tokenPropertyName),
                     'Accept' : 'application/json'}};           

}

function IsTokenValid() {
return UserProperties.getProperty(tokenPropertyName);
}

显示的错误是:错误请求:未定义

我认为错误在名为 getAndStoreAccessToken 的函数内部。

var parameters = { method : 'post',
payload : 'client_id='+ CLIENT_ID + '&client_secret=' + CLIENT_SECRET + '&grant_type=authorization.code&redirect_uri=' + REDIRECT_URL};

请告诉我有效载荷的正确 url 格式。

4

2 回答 2

1

错误似乎在这一行 -

var response = UrlFetchApp.fetch(TOKEN_URL.parameters).getContentText();

我想你想要TOKEN_URL , parameters(注意逗号)

于 2013-04-01T19:14:55.240 回答
0

首先,如果您尝试从 Google Apps 脚本中访问 Google Drive,授权的目的是什么?无需授权即可使用 Google 云端硬盘。您是否试图让您的应用程序利用其他用户(或代表其他用户)的 gDrive?

其次,您可以利用OAuthConfig 类简化授权/请求过程,而不是手动执行授权,这很难排除故障。唯一的缺点是 OAuthConfig 当前使用 OAuth1.0(目前已弃用)。虽然它的特殊用途是 Fusion Tables,而不是驱动器,但这个库充分利用了 OAuthConfig 和 .fetch,我已经用它来模拟我自己的 OAuth 函数。我下面的例子效果很好。该googleAuth()功能设置授权,然后应用程序的其余部分可以使用UrlFetchApp.fetch(url,options)Google 在后台执行所有授权操作来发出授权请求。

function googleAuth(oAuthFields) {
      var oAuthConfig = UrlFetchApp.addOAuthService(oAuthFields.service);
      oAuthConfig.setRequestTokenUrl("https://www.google.com/accounts/"+
                 "OAuthGetRequestToken?scope=" + oAuthFields.scope);
      oAuthConfig.setAuthorizationUrl("https://www.google.com/accounts/OAuthAuthorizeToken");
      oAuthConfig.setAccessTokenUrl("https://www.google.com/accounts/OAuthGetAccessToken");
      oAuthConfig.setConsumerKey(oAuthFields.clientId);
      oAuthConfig.setConsumerSecret(oAuthFields.clientSecret);
      return {oAuthServiceName:oAuthFields.service, oAuthUseToken:"always"};
}
function fusionRequest(methodType, sql, oAuthFields, contentType) {
  var fetchArgs = OAL.googleAuth(oAuthFields);
  var fetchUrl = oAuthFields.queryUrl;
  fetchArgs.method = methodType;
  if( methodType == 'GET' ) {
    fetchUrl += '?sql=' + sql;
    fetchArgs.payload = null;
  } else{
    fetchArgs.payload = 'sql='+sql;
  }
  if(contentType != null) fetchArgs.contentType = contentType;
  Logger.log(UrlFetchApp.getRequest(oAuthFields.queryUrl, fetchArgs));
  var fetchResult = UrlFetchApp.fetch(oAuthFields.queryUrl, fetchArgs);

  if( methodType == 'GET' ) return JSON.parse(fetchResult.getContentText());
  else return fetchResult.getContentText();

}
于 2013-03-31T01:16:46.527 回答