2016-07-07 28 views
1

處分配無效的左側嘗試完成OAuth2流,但繼續獲取未捕獲的引用錯誤。對Node.js來說是相當新穎的東西,似乎無法找出發生了什麼。ReferenceError:在對象

// require the blockspring package. 
var blockspring = require('blockspring'); 
var request = require('request'); 

// pass your function into blockspring.define. tells blockspring what function to run. 
blockspring.define(function(request, response) { 

    // retrieve input parameters and assign to variables for convenience. 
    var buffer_clientid = request.params["buffer_clientid"]; 
    var buffer_secret = request.params["buffer_secret"]; 
    var redirectURI = request.params["redirectURI"]; 
    var tokencode = request.params["tokencode"]; 


    request({ 
     method: "POST", 
     url: "https://api.bufferapp.com/1/oauth2/token.json", 
     headers: { 
     'User-Agent': 'request', 
     }, 
     body: client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code 

    }, function(error, response, body){ 
     console.log(body); 

     // return the output. 
     response.end(); 
    }); 
}); 
+1

你需要把你的左右'body'數據報價。 'client_id = buffer ...'應該是一個字符串。您正在嘗試將某些內容分配給不存在的'client_id'。 –

+0

請求對象中的主體鍵需要格式化爲一個字符串。你或者需要用''string'+ variable +'string''來連接 – FrankerZ

回答

1

這不是有效的JavaScript語法:

body: client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code 

我假設你正在嘗試來連接你的變量值的字符串?試試這個:

body: "client_id=" + buffer_clientid + "&client_secret=" + buffer_secret + "&redirect_uri=" + redirectURI + "&code=" + tokencode + "&grant_type=" +authorization_code 
0

需要引用nodej中的字符串。在你的請求函數中,你傳遞了一個正文的關鍵字,其值是一個看起來巨大的變量。由於client_id=buffer_clientid&client_secret=buffer_secret&redirect_uri=redirectURI&code=tokencode&grant_type=authorization_code附近沒有引號,因此它試圖將此視爲一個變量。當解析器到達=符號時,它將嘗試設置client_id =以下內容。這是拋出錯誤。

只需引用整個字符串或者如果您需要使用變量concat使用'string' + variable + 'string'

您的變量名稱來看,就可以簡單的把它改寫如下:

request({ 
    method: "POST", 
    url: "https://api.bufferapp.com/1/oauth2/token.json", 
    headers: { 
    'User-Agent': 'request', 
    }, 
    body: 'client_id=' + buffer_clientid + '&client_secret=' + buffer_secret + '&redirect_uri=' + redirectURI + '&code=' + tokencode + '&grant_type=authorization_code' 

}, function(error, response, body){ 
    console.log(body); 

    // return the output. 
    response.end(); 
})