2

I successfully generated an access token but now I can't use it. Sadly there is no official yandex node library for using the API. There are 2 unofficial npm module that will only work with a server but I want it localhost. As example with code like this, in this case I want to display all files from my disk. Of course I enabled all scopes for my app

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

Also if I would use https://cloud-api.yandex.net/v1/disk/resources/files?oauth_token=xxxxxxxxxxxxxx&oauth_client_id=xxxxxxxxxxxxxxxxxxx
or https://cloud-api.yandex.net/v1/disk/resources/files?access_token=xxxxxxxxxxxxxx&client_id=xxxxxxxxxxxxxxxxxxx
in my browser I would get
{"message":"?? ???????????.","description":"Unauthorized","error":"UnauthorizedError"}

Somebody has working code or an idea why I´am getting this message?

Programah
  • 179
  • 1
  • 10
t33n
  • 270
  • 1
  • 3
  • 17

2 Answers2

2

Now I'm using yandex-disk package. It works enougth for my purposes.

That my steps:

  1. Register app in Yandex Oath Service. After that I can get info about my app
  2. Realize the access method to get Oath Token by calling this.
  3. npm i yandex-disk
  4. Inserting the code into 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);
    });
    
Pax Beach
  • 2,059
  • 1
  • 20
  • 27
0

Just include authorization header like that:

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