我正在使用plugin将linkedin登录集成到我的应用中。但是,在获取访问令牌后尝试从linkedin获取基本配置文件时,我目前面临一个问题。
<LinkedInModal
ref={ (linkedInModal) => {
this.linkedinButton = linkedInModal;
}}
permissions={['r_basicprofile', 'r_emailaddress', 'rw_company_admin', 'w_share']}
clientID="secret"
clientSecret="secret"
redirectUri="secret"
renderButton={this.renderLinkedInButton.bind(this)}
onSuccess={
token => {
console.log(token);
axios
.get('https://api.linkedin.com/v1/people/~?format=json')
.then(user => console.log(user))
.catch(error => console.log(error));
}
}
onSignIn={user => console.log(user)}
/>
虽然我能够成功获得令牌。但是axios.get()
会返回错误: - Request failed with status code 401
。关于我做错了什么想法?
答案 0 :(得分:1)
尝试使用您获得的令牌提供Authorization标头:
onSuccess = {
token => {
console.log(token);
let config = {
headers: {
'Authorization': `Bearer ${token}` //ES6 template string, u may also use '+'
}
};
axios
.get('https://api.linkedin.com/v1/people/~?format=json', config)
.then(user => console.log(user))
.catch(error => console.log(error));
}
}
这是Google登录API的方式,不知道它是否有效。如果有,请告诉我:))
答案 1 :(得分:0)
onSuccess
使用access_token
键将令牌作为对象返回。因此,如果您将令牌添加到请求标头(如下所示),它将起作用。
<LinkedInModal
onSuccess={
token => {
console.log(token.access_token);
axios
.get('https://api.linkedin.com/v1/people/~?format=json',
{
headers: {
Authorization: "Bearer " + token.access_token,
"Content-Type": "application/json",
"x-li-format": "json"
}
}
)
.then(user => console.log(user))
.catch(error => console.log(error));
}
}
/>