2015-06-26 34 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”内),然后它应该可以工作。

相关问题