0
votes

I have a folder in which I want to insert a file....File is a image whose url I have provided in photoURL variable My code :

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)
}

It does not inset image....What changes should i have to do ????

It gives error :

Request failed for returned code 403. Server response: { "error": { "errors": [ { "domain": "usageLimits", "reason": "dailyLimitExceededUnreg", "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup.", "extendedHelp": "https://code.google.com/apis/console" } ], "code": 403, "message": "Daily Limit for Unauthenticated Use Exceeded. Continued use requires signup." } }

1
seing "Continued use requires signup" are you using a functional authentication ? - Serge insas
Was looking for the code snippet which describes how to specify parents. above code worked out for me. :) - Amit

1 Answers

0
votes

This is because of your access token issue, you have to get access token for 'https://www.googleapis.com/auth/drive.file'

download latest google api console and try as per documents.