1) Get oauth 2.0 token with offline access for the following scope: "https://www.googleapis.com/auth/books"
2) Confirm it works to access user data at the following API end-point: https://www.googleapis.com/books/v1/mylibrary/bookshelves
3) Try adding a book with the following end-point: https://books.googleapis.com/books/v1/cloudloading/addBook
4) Get a 401 error, although the token is valid.
Code example
from googleapiclient.discovery import build
import google.auth.transport.requests as refresh
from google.oauth2.credentials import Credentials
import sys
id=sys.argv[1]
def upload_epub_to_play_books():
# Load the OAuth 2.0 credentials from a file
creds = Credentials.from_authorized_user_file('/home/user/scripts/playbooks/credentials.json')
# if not creds.valid:
# if creds.refresh_token:
# request = refresh.Request()
# creds.refresh(request)
# if creds.valid:
# print("Creds valid and were refreshed")
# with open('/home/user/scripts/playbooks/credentials.json', 'w') as token:
# token.write(creds.to_json())
# else:
# print("creds already valid")
# Create a Google Play Books API service client
service = build('books', 'v1', credentials=creds)
# Upload the epub file to Google Play Books
response = service.cloudloading().addBook(drive_document_id=id).execute()
print(response)
# Upload an epub file to Google Play Books
upload_epub_to_play_books()
Stack trace
Traceback (most recent call last):
File "/home/user/scripts/playbooks/add_to_playbooks.py", line 33, in <module>
upload_epub_to_play_books()
File "/home/user/scripts/playbooks/add_to_playbooks.py", line 28, in upload_epub_to_play_books
response = service.cloudloading().addBook(drive_document_id=id).execute()
File "/home/user/.local/lib/python3.9/site-packages/googleapiclient/_helpers.py", line 130, in positional_wrapper
return wrapped(*args, **kwargs)
File "/home/user/.local/lib/python3.9/site-packages/googleapiclient/http.py", line 938, in execute
raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 401 when requesting https://books.googleapis.com/books/v1/cloudloading/addBook?drive_document_id=1QBcMAaQktHT-LmqKDM90ub0XL3IRzBNW&alt=json returned "Invalid Credentials". Details: "[{'message': 'Invalid Credentials', 'domain': 'global', 'reason': 'authError', 'location': 'Authorization', 'locationType': 'header'}]">
This is really strange, because the script was working just a few months ago. It suddenly stopped working and giving 401 errors. I am 100% confident that the credentials/token are not invalid.
Environment details
google-api-python-client
version: 2.141.0Steps to reproduce
1) Get oauth 2.0 token with offline access for the following scope: "https://www.googleapis.com/auth/books" 2) Confirm it works to access user data at the following API end-point:
https://www.googleapis.com/books/v1/mylibrary/bookshelves
3) Try adding a book with the following end-point: https://books.googleapis.com/books/v1/cloudloading/addBook 4) Get a 401 error, although the token is valid.Code example
Stack trace
This is really strange, because the script was working just a few months ago. It suddenly stopped working and giving 401 errors. I am 100% confident that the credentials/token are not invalid.