2013-03-07 17 views
0

我想將我的應用程序連接到foursquare,並且我想在用戶登錄到某些地方時顯示一條消息。我試圖使用他們的實時api https://developer.foursquare.com/overview/realtimeFoursquare簽入回覆

一切工作正常,直到最後,(當我必須發送回覆帖子請求https://developer.foursquare.com/docs/checkins/reply)我使用express和node.js.這是我的發佈請求的樣子。

app.post('/handlepush', function(req, res) { 
var checkin_id =req.param('checkin'); 
console.log(checkin_id); 
var obj = JSON.parse(checkin_id); 
var id = obj.id; 

res.end('It worked!'); 

var token = "********************************"; 

var post_data = querystring.stringify({text : "awesome"}); 
var options = { 
    host: 'api.foursquare.com', 
    path: '/v2/checkins/' + id + '/reply?oauth_token=' + token, 
    port: 443, 
    method: 'POST' 
}; 

var req2 = https.request(options, function(res2) { 
    res2.setEncoding('utf8'); 
    res2.on('data', function (chunk) { 
     console.log('BODY: ' + chunk); 
    }); 

req2.on('error', function(e) { 
    console.log('problem with request: ' + e.message); 
}); 

}); 
req2.write(post_data); 
req2.end(); 



}); 

這是我的錯誤,由於某種原因,我不能添加參數爲我的帖子: BODY:{「元」:{「代碼」:400,「錯誤類型」:「其他」 ,「errorDetail」:「必須提供參數文本」},「response」:{}}

+0

任何人都可以提供一個示例API調用,如果我的問題? – 2013-03-07 05:16:16

回答

1

您需要實際發送您的請求。見:How to make an HTTP POST request in node.js?

var req2 = http.request(options, function(res2) { 
    res2.setEncoding('utf8'); 
    res2.on('data', function (chunk) { 
     console.log('BODY: ' + chunk); 
    }); 
}); 
req2.end(); 
+0

所以我改變了一下,現在它幾乎工作..但我只需要找出一種方法來添加參數。 – 2013-03-07 17:47:13

+1

「text」應該是查詢字符串中的一個參數: 'path:'/ v2/checkins /'+ id +'/ reply?oauth_token ='+ token +'&text = Text%20to%20show%20user,' – octopi 2013-03-07 18:33:56

+0

有效的工作!但我有點困惑,我認爲後期參數不應該在網址..? – 2013-03-07 20:15:36

相關問題