roopakv / google-photos

Library to make hitting the Google Photos api easy
MIT License
56 stars 22 forks source link

I don't know how to specify the token for method Photos() ? #18

Closed janvda closed 4 years ago

janvda commented 4 years ago

Most likely my implementation is not correct:

An extract of the relevant part of my code:

const Photos = require('googlephotos');
...
node.photos = new Photos(node.config.credentials.accessToken);
... 
async function get_albums_list(node){
                node.warn("get_albums_list ...")
                let response = await node.photos.albums.list();
                node.warn("get_albums_list response :"+ response);
                alert(response);
            }

get_albums_list(node);

FYI node.config.credentials.accessToken is "ya29.Il-zB8z2qrcP7qP3YVpUlt8NCsvb1ZxtsIHTlPi71HN30wFLJs7c5q...".

when running the above code I am getting error

(node:16) UnhandledPromiseRejectionWarning: StatusCodeError: 401 - {"error":{"code":401,"message":"Request had invalid authentication credentials. Expected OAuth 2 access token, login cookie or other valid authentication credential. See https://developers.google.com/identity/sign-in/web/devconsole-project.","status":"UNAUTHENTICATED"}}
    at new StatusCodeError (/data/node_modules/request-promise-core/lib/errors.js:32:15)
    at Request.plumbing.callback (/data/node_modules/request-promise-core/lib/plumbing.js:104:33)
    at Request.RP$callback [as _callback] (/data/node_modules/request-promise-core/lib/plumbing.js:46:31)
    at Request.self.callback (/data/node_modules/request/request.js:185:22)
    at Request.emit (events.js:198:13)
    at Request.<anonymous> (/data/node_modules/request/request.js:1161:10)
    at Request.emit (events.js:198:13)
    at IncomingMessage.<anonymous> (/data/node_modules/request/request.js:1083:12)
    at Object.onceWrapper (events.js:286:20)
    at IncomingMessage.emit (events.js:203:15)
    at endReadableNT (_stream_readable.js:1143:12)
    at process._tickCallback (internal/process/next_tick.js:63:19)
(node:16) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2)
mcpengelly commented 4 years ago

This is more of a question for googleapis. Heres a link to their code examples: https://github.com/googleapis/google-api-nodejs-client/blob/master/samples/sheets/quickstart.js

An example of how I used it with googlephotos api: https://github.com/mcpengelly/instagram-saved-to-google-photos-album/blob/master/oauth.ts

Also, I've adapted their code here. but i suggest using their example because this code is incomplete:

app.get('/oauth2callback', (req, res) => {
  const code = req.query.code;
  client.getToken(code, (err, tokens) => {
    ...
    client.credentials = tokens;
    doSomethingWithToken(client);
    ...
  })
})

function doSomethingWithToken(client) {
    new Photos(client.credentials.access_token)
    ...
}
janvda commented 4 years ago

Thanks for the very fast response.

Based on your input, I further looked into it. My code was correct but the access token (string) that I passed to new Photos() was apparently no longer valid.

I have created new credentials and generated new tokes using these credentials and that resolved the issue.