2015-06-26 29 views
0

我對此非常陌生,目前正在努力獲取OAuth 2.0令牌以用於Google Apps腳本以寫入Fusion Table。我使用的是Arun的Google Developers Live代碼,我似乎無法獲取訪問令牌。當我運行下面的doGet函數時,它給我一個「類型錯誤:無法讀取屬性」參數「來自未定義」。獲取OAuth2的參數錯誤

function doGet(e) { 
    var HTMLToOutput; 
    if(e.parameters.code){//if we get "code" as a parameter in, then this is a callback. we can make this more explicit 
    getAndStoreAccessToken(e.parameters.code); 
    HTMLToOutput = '<html><h1>Finished with oAuth</h1>You can close this window.</html>'; 
    } 
    return HtmlService.createHtmlOutput(HTMLToOutput); 
} 

function getAndStoreAccessToken(code){ 
    var parameters = { 
    method : 'post', 
    payload : 'client_id='+CLIENT_ID+'&client_secret='+CLIENT_SECRET+'&grant_type=authorization_code&redirect_uri='+REDIRECT_URL+'&code=' + code 
    }; 

    var response = UrlFetchApp.fetch(TOKEN_URL,parameters).getContentText(); 
    var tokenResponse = JSON.parse(response); 

    // store the token for later retrieval 
    UserProperties.setProperty(tokenPropertyName, tokenResponse.access_token); 
} 

任何幫助將不勝感激。

回答

0

在Appsscript中有一些觸發器,這些觸發器響應某些動作或參數執行一段代碼。

在這種情況下,您正在使用觸發器doGet(這是您的函數的名稱)。如您所見,該函數接收參數「e」。如果你在環境中直接運行該函數,這個參數將是「未定義的」,因爲你沒有傳遞任何東西給函數。

當您將代碼作爲Web應用程序訪問時,會執行此觸發器。要做到這一點,你必須點擊「保存」按鈕旁邊的圖標(看起來像一個帶箭頭的雲)here你可以找到信息。

當您通過部署應用程序後獲得的url訪問代碼時,該函數接收必要的參數(在「e」內),然後它應該可以工作。

相關問題