2017-09-15 49 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) 这似乎是请求库中的错误。请注意,没有标题部分,它只是给了我在原始文章中提到的错误。有任何想法吗?谢谢 –