【问题标题】:Yandex Disk API can't make request with access token node.jsYandex Disk API 无法使用访问令牌 node.js 发出请求
【发布时间】:2018-01-19 03:54:48
【问题描述】:

我成功生成了访问令牌,但现在我无法使用它。遗憾的是,没有用于使用 API 的官方 yandex 节点库。有 2 个非官方的 npm 模块只能与服务器一起使用,但我希望它是 localhost。作为这样的代码示例,在这种情况下,我想显示磁盘中的所有文件。当然,我为我的应用启用了所有范围

request.get('https://cloud-api.yandex.net/v1/disk/resources/files/', {
  'auth': {
    'bearer': 'xxxxxxxxxxxxxxxxxxx'
  }
}, function(err,httpResponse,body){ /* ... */ 
if(err) {
console.log('err: ' + err)
}
console.log('body: ' + body)
});

如果我使用https://cloud-api.yandex.net/v1/disk/resources/files?oauth_token=xxxxxxxxxxxxxx&oauth_client_id=xxxxxxxxxxxxxxxxxxx
https://cloud-api.yandex.net/v1/disk/resources/files?access_token=xxxxxxxxxxxxxx&client_id=xxxxxxxxxxxxxxxxxxx
在我的浏览器中,我会得到
{"message":"?? ???????????.","description":"Unauthorized","error":"UnauthorizedError"}

有人有工作代码或知道我收到此消息的原因吗?

【问题讨论】:

    标签: node.js yandex yandex-api


    【解决方案1】:

    现在我正在使用yandex-disk 包。它足以满足我的目的。

    我的步骤:

    1. Register app 在 Yandex 宣誓服务中。 之后我可以get info 了解我的应用程序
    2. 实现获取Oath Tokenby calling this的访问方式。
    3. npm i yandex 磁盘
    4. 将代码插入 index.js

      const YandexDisk = require('yandex-disk').YandexDisk;
      const disk = new YandexDisk('YourOathTokenHere');
      console.log('disk', disk);
      disk.readdir('/', (err, response) => {
          console.log('callback error', err);
          console.log('callback response', response);
      });
      

    【讨论】:

      【解决方案2】:

      只需像这样包含授权标头:

      axios.get(encodeURI(`https://cloud-api.yandex.net:443/v1/disk/resources?path=${path}`), {
              headers: {
                  'Authorization': `OAuth ${token}`
              }
          }).then((res) => {
              console.log(res);
          }).catch((err) => {
              console.log(err);
          });
      

      【讨论】:

        猜你喜欢
        • 2018-08-22
        • 2019-02-19
        • 1970-01-01
        • 2013-02-17
        • 2021-11-27
        • 2019-03-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多