【问题标题】:How to insert a file in folder using Drive API in google Script如何在谷歌脚本中使用 Drive API 在文件夹中插入文件
【发布时间】:2013-02-06 11:12:16
【问题描述】:

我有一个文件夹,我想在其中插入一个文件....文件是一个图像,其 url 我在 photoURL 变量中提供 我的代码:

var consumerKey="yourDomain.com";
var consumerSecret="yourSecretKey";
function image() {
         // Photo url
         var photoURL="photourl"

         // Getting content of Image
         var imageContent=UrlFetchApp.fetch(photoURL).getContent()      

         // Storing image into drive
       storeIntoDrive(imageContent)      
}



// Oauth Authentication
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"};
}



//  To store image into Google Drive
function storeIntoDrive(content){
  var base="https://www.googleapis.com/auth/drive.file"
  var url='https://www.googleapis.com/upload/drive/v2/files?uploadType=media'
  var fetchArgs=googleOAuth_('drives',base)
  fetchArgs.payload=content
  fetchArgs.method='POSt'
  fetchArgs.contentType="images/jpeg"
  fetchArgs.header={"title" : "demo", "mimeType" : "image/jpeg",
  "parents": [{
    "kind": "drive#fileLink",
    "id": "0B3qF7GcD2uDHc2J5d21haFJFc0k"
  }]}
  var result=UrlFetchApp.fetch(url,fetchArgs)
}

它没有插入图像....我应该做些什么改变????

它给出了错误:

返回代码 403 的请求失败。服务器响应:{“error”:{“errors”:[{“domain”:“usageLimits”,“reason”:“dailyLimitExceededUnreg”,“message”:“未验证的每日限制使用已超过。继续使用需要注册。", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "每日未验证使用限制已超过。继续使用需要注册。" } }

【问题讨论】:

  • 看到“继续使用需要注册”你使用的是功能认证吗?
  • 正在寻找描述如何指定父母的代码 sn-p。上面的代码为我解决了。 :)

标签: google-apps-script google-drive-api


【解决方案1】:

这是因为您的访问令牌问题,您必须获取“https://www.googleapis.com/auth/drive.file”的访问令牌

下载最新的google api控制台并按照文档尝试。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多