【问题标题】:Why am I still getting a 401(Unauthorized) error from api get request?为什么我仍然从 api 获取请求中收到 401(未经授权)错误?
【发布时间】:2019-01-31 06:00:44
【问题描述】:

我有一个尝试从频道中获取一些视频以使用 axios 做出反应并获取对 api 的请求的示例。一切看起来都不错,但是当它编译时,我收到一个控制台错误,指出:GET 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; 

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

【问题讨论】:

  • 我可能错了,但我认为 url 有一个额外的 slash.axios.get(https://api.vimeo.com/channels/180097/videos/&amp;key=${API_KEY}) -- 应该是 axios.get(https://api.vimeo.com/channels/180097/videos&amp;key=${API_KEY}) 对吧?
  • 您不应该在标头而不是 URL 中发送访问令牌吗? developer.vimeo.com/api/authentication#best-practices
  • @MartinBroadhurst 我只是按照命令行中的文档所述进行操作,它给了我:Invoke-WebRequest:无法绑定参数“标头”。无法将“System.String”类型的“Authorization: Bearer *****************”值转换为“System.Collections.IDictionary”类型。
  • @KirkLarkin 我只是按照命令行中的文档所述进行操作,它给了我:Invoke-WebRequest:无法绑定参数“标头”。无法将“System.String”类型的“授权:Bearer *****************”值转换为“System.Collections.IDictionary”类型。我不知道为什么

标签: javascript reactjs api get axios


【解决方案1】:

您首先需要向https://api.vimeo.com/oauth/authorize/client 发出帖子请求,并将Authorization 标头设置为Basic Auth,您的用户名是您的应用程序client identifier,密码是your client secret。所以Authentication: Basic base64(&lt;client-identifier&gt;:&lt;client-secret&gt;)。您还需要将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 &lt;access_token&gt;

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 中的 Postman 导出:

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);

【讨论】:

  • 非常感谢。
  • 您需要使用更多信息更新您的问题,以查看您在哪里遇到问题。上面的请求是使用 Postman 构建的,所以它应该更多是一个实现问题。
  • 这不是问题,postman是一个测试HTTP请求的工具。如果它在那里有效,那么您的问题就在其他地方。但是如果没有看到您的代码,我无法帮助您。
  • 好吧好吧。这是代码的一部分:jsfiddle.net/mlegemah/n5u2wwjg/157040
  • 您的小提琴中没有任何 JavaScript 代码。我用codesandbox.io/s/pwro9p7w8j 的例子做了一个沙箱。渲染功能可能会损坏,因为 vimeo api 在太多请求后暂时阻止了我,我无法对其进行测试。在文件顶部输入您的客户标识符和 client_secret。
猜你喜欢
  • 2010-10-03
  • 2020-12-23
  • 1970-01-01
  • 1970-01-01
  • 2022-07-18
  • 1970-01-01
  • 1970-01-01
  • 2016-02-06
  • 1970-01-01
相关资源
最近更新 更多