首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何通过文档id下载文档

如何通过文档id下载文档
EN

Stack Overflow用户
提问于 2012-09-25 15:02:43
回答 1查看 778关注 0票数 0
代码语言:javascript
复制
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数组作为文档&我正在使用出口格式作为docs....So,请给出一些建议,这个代码.....当我运行此代码时,它显示错误: error 302..Moved temporarily..

EN

回答 1

Stack Overflow用户

发布于 2012-09-25 22:21:31

试试这个:

代码语言:javascript
复制
 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)

好吧,祝你好运,

托马斯·范·拉塔姆

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12577772

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档