2013-02-06 3 views
0

파일을 삽입 할 폴더가 있습니다 .... 파일의 URL은 photoURL 변수에서 제공 한 URL입니다. 내 코드 :Google의 드라이브 API를 사용하여 폴더에 파일을 삽입하는 방법

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) 서버 응답 실패 : { "오류": { "오류": [{ "도메인": "usageLimits", "이유": "dailyLimitExceededUnreg" "message": "Unauthenticated Use의 일일 한도 초과"계속되는 사용에는 가입이 필요합니다. ","extendedHelp ":"https://code.google.com/apis/console "}],"code ": 403,"message ":"Unauthenticated Use의 일일 한도 초과. 가입. " }}

+0

"계속 사용하려면 가입 필요"기능 인증을 사용하고 있습니까? –

+0

부모를 지정하는 방법을 설명하는 코드 스 니펫을 찾고있었습니다. 위의 코드는 저에게 효과적이었습니다. :) – Amit

답변

0

이 때문에 액세스 토큰 문제로, 당신은 'https://www.googleapis.com/auth/drive.file'

다운로드 최신 구글 API를 콘솔에 대한 액세스 토큰을 얻을 수 및 문서에 따라 시도해야합니다.

관련 문제