[英]Cant get access_token
I get my body with access_token
but I want to use the only access_token
from the body. 我用
access_token
获取我的身体,但我想使用身体中唯一的access_token
。 But give me access_token
is undefined 但是给我
access_token
是未定义的
request(options, function (error, response, body) {
if (error) throw new Error(error);
console.log(body.access_token);
}
There are some reasons that could make this issue. 有一些原因可以解决这个问题。
If your option doesn't have json: true
like this: 如果您的选项没有
json: true
就像这样:
request({ uri: 'https://jsonplaceholder.typicode.com/todos/1' }, (error, response, body) => { if (error) throw new Error(error); console.log(typeof body) //string })
So that you can put json: true
to the option, body will be ojbect
. 这样你就可以将
json: true
放到选项中,body将是ojbect
。 The code will look like this: 代码如下所示:
request({ uri: 'https://jsonplaceholder.typicode.com/todos/1', json: true }, (error, response, body) => { if (error) throw new Error(error); console.log(typeof body) //object })
If you set up option
with json: true
and the body still is a string. 如果你用
json: true
设置option
,并且正文仍然是一个字符串。 I think your API might response a wrong format. 我认为您的API可能会响应错误的格式。 Like you commented above:
像你上面评论的那样:
{ "access_token":"1231231313123312", "token_type":"Bearer", "expires_in":3600, }
It may be due to the last comma which leads to couldn't parse your response to an object. { "access_token":"1231231313123312", "token_type":"Bearer", "expires_in":3600, }
可能是由于最后一个逗号导致无法解析对象的响应。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.