2017-09-15 103 views
1

我試圖訪問Yelp融合API。我下面的documentation,來到這個代碼:Yelp融合:無法獲取令牌

const request = require('request'); 

// As you can see the common error of there being a whitespace 
// in one of these is not the case here 
const clientId = 'O<removed>w'; 
const clientSecret = 'm<removed>r'; 

const options = { 
    url: 'https://api.yelp.com/oauth2/token', 
    method: 'POST', 
    headers: { 
     'Content-Type': 'application/x-www-form-urlencoded' 
    }, 
    json: { 
     'grant_type': 'client_credentials', 
     'client_id': clientId, 
     'client_secret': clientSecret 
    } 
}; 

request(options, (err, res, body) => { 
    if(err) { 
     return console.log(err); 
    } 
    console.log(body); 
}); 

使用API​​Gee我得到這個確切的同一調用正確的響應,但是在我的OVH VPS我得到這個錯誤:(注意,這是從身體變量)

{ error: 
    { code: 'VALIDATION_ERROR', 
     description: 'client_id or client_secret parameters not found. Make sure to provide client_id and client_secret in the body with the application/x-www-form-urlencoded content-type' } } 

我相信我正確設置了電話。任何人都可以指出我正確的方向嗎?謝謝!

+0

看[這裏](https://stackoverflow.com/questions/9870523/differences-in-application-json-and-application-x-www-form-urlencoded) –

+0

燦你用小寫檢查'content-type'? – abdulbarik

+0

@abdulbarik沒有工作 –

回答

0

您需要更改選項才能正確提交數據。數據進入body領域,如果它是一個JSON對象,而不是你需要設置json:true

const options = { 
    url: 'https://api.yelp.com/oauth2/token', 
    method: 'POST', 
    body: { 
     'grant_type': 'client_credentials', 
     'client_id': clientId, 
     'client_secret': clientSecret 
    }, 
    json: true 
}; 

也是一個字符串,頭是必需的,我不認爲。

檢查請求options documentation

+0

我現在有[此代碼](https://hastebin.com/hokexidixa.scala) 我得到[此錯誤](https://hastebin.com/konifaxana。 vbs) 這似乎是請求庫中的錯誤。請注意,沒有標題部分,它只是給了我在原始文章中提到的錯誤。有任何想法嗎?謝謝 –

相關問題