Authentication session is not defined
Asked Answered
P

3

6

I try to use Google Photos API to upload my images, base on the steps of the following link.

https://developers.google.com/photos/library/guides/upload-media

After following the Using OAuth 2.0 for Web Server Applications, I just get the Oauth2.0_token response(a JSON format with access_token, refresh_token...). However, after I put this token string with "Bearer " into request headers, the response is error 401, the error message is "code 16 Authentication session is not defined".

I cannot find any information to deal with it, thank for any help.

Prognosis answered 30/8, 2018 at 8:26 Comment(0)
N
2

You probably have incorrect permissions. Make sure you request the token with the appropriate scope. For write-only access you need 'https://www.googleapis.com/auth/photoslibrary.appendonly'

src: https://developers.google.com/photos/library/guides/authentication-authorization#what-scopes

Ninnetta answered 30/8, 2018 at 20:34 Comment(3)
Thanks for your reply! I use googleapis.com/auth/photoslibrary, which should contain append and read. But this situation still happens. imgur.com/a/ZgcjF3cPrognosis
@user8590209 did you find any solution? I have the same problem.Photocell
@user1216249 No, I change my language to python and everything goes fine. I have totally no idea why and how this happened.Prognosis
J
0

One reason this might be happening is that you initially authorized your user for read-only access. If you went through the authorization flow with a .readonly scope, your bearer token reflects that authorization (and the token is retained in your credentials file). If you change your scope but don't get a new auth token you will get this error when trying to upload. Simply redo the authorization flow with the new scope defined:

SCOPES = 'https://www.googleapis.com/auth/photoslibrary'
store = file.Storage('path_to_store')
if not creds or creds.invalid:
    flow = client.flow_from_clientsecrets('google_credentials.json', SCOPES)
    creds = tools.run_flow(flow, store)

and your store will be populated with a new token that can be used for uploading.

Jato answered 11/3, 2019 at 15:33 Comment(0)
C
0

You say you "just get the Oauth2.0_token response(a JSON format with access_token, refresh_token...)" and "put this token string with "Bearer " into request headers".

Unfortunately documentation on this isn't super clear in a lot of places. What you are supposed to provide after "Bearer" is the "access_token" field only, not the entire JSON string with all the token fields in it. For reference, this is a single string of random looking characters which probably starts with "ya29." and is pretty long - in my case it's 170 characters.

Chlorinate answered 24/4, 2020 at 1:9 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.