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.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
Chao
  • 179
  • 1
  • 8

3 Answers3

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

  • 1
    Thanks for your reply! I use https://www.googleapis.com/auth/photoslibrary, which should contain append and read. But this situation still happens. https://imgur.com/a/ZgcjF3c – Chao Aug 31 '18 at 02:20
  • 1
    @user8590209 did you find any solution? I have the same problem. – Michael Offengenden Jan 23 '19 at 07:46
  • @user1216249 No, I change my language to python and everything goes fine. I have totally no idea why and how this happened. – Chao Apr 01 '19 at 04:17
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.

llevar
  • 755
  • 8
  • 24
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.

Michael
  • 9,060
  • 14
  • 61
  • 123