0
function getDocuments(user){
  var scope = 'https://docs.google.com/feeds/';
  //oAuth
  user="user@yourdomain.com"
  var fetchArgs = googleOAuth_('docs', scope);
  var url = scope + user+'/private/full?v=3&alt=json';
  var urlFetch = UrlFetchApp.fetch(url, fetchArgs);
  var json=Utilities.jsonParse(urlFetch.getContentText())
  var entry = json.feed.entry;
  var docs = [];
  for(var i in entry){
    var tempDoc = {};
    for(var j in entry[i]){
      tempDoc.id = entry[i].id.$t.split('%3A')[1];
    }
    docs.push(tempDoc);
  }

  var url='https://docs.google.com/feeds/download/documents/Export?docID='+docs[0]+'&exportFormat=docs'
UrlFetchApp.fetch(url,fetchArgs)

}

//--------------------------------------------------------------------------------------
//Google oAuth
//Used by getDocuments(user)
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",method:"GET"};
}

我正在尝试通过文档 ID 下载文档的上述代码。我有一个文档 ID 数组作为文档,我正在使用导出格式作为文档......所以请对此代码提出一些建议......当我运行此代码时,它会给出错误:错误 302 ..临时移动。 .

4

1 回答 1

0

尝试这个:

 for(var i in entry){
    var tempDoc = {};
    for(var j in entry[i]){
      tempDoc.id = entry[i].id.$t.split('%3A')[1];
    }
//now your splitting your string nicely but you also want to use that
    docs.push(tempDoc.id);
  }

  var url='https://docs.google.com/feeds/download/documents/Export?docID='+docs[0]+'&exportFormat=docs'

//you dont want to use the fetch arguments anymore you have authenticated already
UrlFetchApp.fetch(url)

祝你好运,

托马斯·范拉图姆

于 2012-09-25T14:21:31.093 回答