为什么我仍然从 api get 请求收到 401(未经授权)错误?



我有一个示例,尝试使用 axios 从 react 中的频道获取一些视频并获取对 API 的请求。一切看起来都不错,但是当它编译时,我收到一个控制台错误,指出:获取https://api.vimeo.com/channels/180097/videos/&key=*********** 401 (Authorization Required

访问令牌是我注册时生成的正确令牌。下面是代码设置:

import React, { Component } from 'react';
import axios from 'axios';
const API_KEY = '***********************';
class Apicall extends Component {
componentDidMount() {
this.getChannel();
}
getChannel() {
axios.get(`https://api.vimeo.com/channels/180097/videos/&key=${API_KEY}`) 
.then(res => {
const videos = res.data.data.children.map(obj => obj.data);
this.setState({videos});
});
}
constructor(props) {
super(props);
this.state = {
channel_id: '180097',
data: [],
videos: [],
per_page: '5',
paging: {
first: '/channels/180097/videos?page=1',
last: '/channels/180097/videos?page=3' 
}
}
this.getChannel = this.getChannel.bind(this);
}
render(){
return (
<div className="container">
<h1></h1>
<ul>
{this.state.videos.map(video => 
<li key={video.uri}></li>
)}
</ul>
</div>
);
}
}
export default Apicall; 

为什么仍然没有获得访问令牌?

您首先需要在Authorization标头设置为Basic Auth的情况下向https://api.vimeo.com/oauth/authorize/client发出 post 请求,您的用户名是您的应用程序client identifier,您的密码是your client secret。所以Authentication: Basic base64(<client-identifier>:<client-secret>).您还需要将grant_type设置为client_credentials

然后,您将收到如下回复:

{
"access_token": "dd339558163d867d92f4616ca06<redacted>",
"token_type": "bearer",
"scope": "public private",
"app": {
"name": "test",
"uri": "/apps/<app_id>"
}
}

然后,该access_token可用于以下请求:

您发出请求 https://api.vimeo.com/channels/180097,Authorization标头设置为Authorization: Bearer <access_token>

Axios将是这样的:

axios.post('https://api.vimeo.com/oauth/authorize/client',
{ grant_type: 'client_credentials' },
{ headers: { Authorization: 'Basic btoa(<client-identifier>:<client-secret>)' } })
axios.get('https://api.vimeo.com/channels/180097',
{ headers: { Authorization: Bearer <access_token>' } })

当然,这花了我一段时间才发现,因为vimeo api文档非常糟糕。

xhr 中的邮递员导出:

var data = "grant_type=client_credentials";
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "https://api.vimeo.com/oauth/authorize/client");
xhr.setRequestHeader("Authorization", "Basic <insert_base64_of_client_id_and_client_secret>");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Postman-Token", "e13df60c-a625-411d-8020-a51086e60838");
xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xhr.send(data);

var data = null;
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("GET", "https://api.vimeo.com/channels/180097");
xhr.setRequestHeader("Authorization", "Bearer <insert_access_token>");
xhr.setRequestHeader("Cache-Control", "no-cache");
xhr.setRequestHeader("Postman-Token", "5f32ac6c-2c86-4fbc-a7cb-43c8b01f7ea7");
xhr.send(data);

最新更新