Yelp Fusion:无法获得令牌

时间:2017-09-15 06:37:01

标签: node.js http-post yelp

我试图访问Yelp fusion API。我正在关注documentation并转到此代码:

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上我收到此错误:(注意这是来自body变量)

{ 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' } }

我相信我正确设置了通话。有人能指出我正确的方向吗?谢谢!

1 个答案:

答案 0 :(得分: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
};

另外,我认为标题不是必需的。

检查请求ENTER_BR