2

I'm trying to use google-api-python-client 1.12.5 with Service account auth under Python 3.8. It seems to me that the when specifying the status parameter, Google responds with a 404 HTTP code. I can't figure out why. I also looked in the docs but I can't relate anything to this error.

I have pasted my code. The error is happening in the third call.

This is the code:

from google.oauth2 import service_account
from googleapiclient.discovery import build

SCOPES = ['https://www.googleapis.com/auth/blogger']
SERVICE_ACCOUNT_FILE = 'new_service_account.json'
BLOG_ID = '<your_blog_id>'

credentials = service_account.Credentials.from_service_account_file(
        SERVICE_ACCOUNT_FILE, scopes=SCOPES)

service = build('blogger', 'v3', credentials=credentials)
p = service.posts()

# FIRST
promise = p.list(blogId=BLOG_ID)
result = promise.execute()

# SECOND
promise = p.list(blogId=BLOG_ID, orderBy='UPDATED')
result = promise.execute()


#THIRD
promise = p.list(blogId=BLOG_ID, orderBy='UPDATED', status='DRAFT')
result = promise.execute()  # <===== ERROR HAPPENS HERE!!!!

service.close()

And this is the traceback:

Traceback (most recent call last):
  File "/home/madtyn/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/202.7660.27/plugins/python/helpers/pydev/pydevd.py", line 1448, in _exec
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/home/madtyn/.local/share/JetBrains/Toolbox/apps/PyCharm-P/ch-0/202.7660.27/plugins/python/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "/home/madtyn/PycharmProjects/blogger/main.py", line 24, in <module>
    result = promise.execute()
  File "/home/madtyn/venvs/blogger/lib/python3.8/site-packages/googleapiclient/_helpers.py", line 134, in positional_wrapper
    return wrapped(*args, **kwargs)
  File "/home/madtyn/venvs/blogger/lib/python3.8/site-packages/googleapiclient/http.py", line 915, in execute
    raise HttpError(resp, content, uri=self.uri)
googleapiclient.errors.HttpError: <HttpError 404 when requesting https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?orderBy=UPDATED&status=DRAFT&alt=json returned "Not Found">
python-BaseException
madtyn
  • 1,469
  • 27
  • 55

1 Answers1

2

I can reproduce this issue... Adding status=DRAFT will return 404 but any other filter is working...

  1. Tried with service account and your code: 404

  2. Tried with API Key like this result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&key=<api_key>'): 404

  3. Extracted "access_token" from service account (credentials.token after a call): result = requests.get('https://blogger.googleapis.com/v3/blogs/<blog_id>/posts?status=DRAFT&orderBy=UPDATED&alt=json&access_token=<extracted_service_account_token>'): 404

But very strangely if I use access_token given by "Try this API" here : https://developers.google.com/blogger/docs/3.0/reference/posts/list?apix_params={"blogId"%3A"blog_id"%2C"orderBy"%3A"UPDATED"%2C"status"%3A["DRAFT"]%2C"alt"%3A"json"} it's works !

Used that token with requests give me my blog post in draft status...

try this api + network panel

Just copy/paste raw Authorization header inside that script:

import requests

blog_id = '<blog_id>'
headers = {
    'Authorization' : 'Bearer <replace_here>'
}

# Using only Authorization header
result = requests.get(
    'https://blogger.googleapis.com/v3/blogs/%s/posts?status=DRAFT&orderBy=UPDATED&alt=json' % (blog_id),
    headers=headers
)
print(result)
# This should print DRAFT if you have at least one draft post
print(result.json()['items'][0]['status'])

# Using "access_token" param constructed with Authorization header splited to have only token
result = requests.get('https://blogger.googleapis.com/v3/blogs/%s/posts?status=DRAFT&orderBy=UPDATED&alt=json&access_token=%s' % (blog_id, headers['Authorization'][len('Bearer '):]))
print(result)
# This should print DRAFT if you have at least one draft post
print(result.json()['items'][0]['status'])

Results I have currently:

enter image description here

The bug doesn't seem to come from the library but rather from the token rights...However I also used the console normally to generate accesses like you.

To conclude I think it's either a bug or it's voluntary from Google... I don't know how long the "Try this API" token is valid but it is currently the only way I found to get the draft articles... Maybe you can try to open a bug ticket but I don't know specifically where it is possible to do that.

Calumah
  • 2,865
  • 1
  • 20
  • 30
  • 2
    In order to create a ticket you can always do so when purchasing a support license. The other place to open such issues would be the [issue tracker](https://developers.google.com/issue-tracker#public_users). – Daniel Ocando Nov 08 '20 at 00:07
  • Hi @DanielOcando. I'm looking the issue tracker, but I don't know which component is the more appropiate for publishing the issue (or if I will have permissions to do so). – madtyn Nov 08 '20 at 16:00
  • 1
    For having already sent an issue here it's quite possible but personally I never got an answer...so good luck. I think that waiting for the solution of the token from the "Try this API" is the only one possible for you :/ – Calumah Nov 08 '20 at 16:19
  • Hello @Calumah, I'm trying to replicate what you have done. I'm entering the "Try this" and using dev tools when pressing "Execute". I can then find a request with alt=json&key=. I assume this last one is the key you are using, but... can I get it from some place using my python code (just in case it expires)? – madtyn Nov 08 '20 at 16:32
  • I tried with that 'AIzaSyxxxxxxxxxx' string from the browser and another one I had from google developers console. None of them worked because they return 403. – madtyn Nov 08 '20 at 16:46
  • 1
    Have you more details about 403 ? Like response body ? Easy way can be use "Copy as curl", removing params to keep only `Authentication` header (remove get param key= as well). I think it's hard to get it automatically without using pupeeter or selenium because it's involving massive js process with oauth, so it's hard I think...but if this feature is really critical you have no other choice at this moment :/ – Calumah Nov 08 '20 at 18:33
  • Could you please just leave me in the answer the lines you tried at the interpreter for making it work? I just want to make sure I didn't anything wrong when following your instructions. I got the access_token after the first call and it didn't work, and then I tried again with the key= with both the key I had and the key from my browser. Everything failed and I prefer non giving up so I must try to look for an error at what i did – madtyn Nov 08 '20 at 18:47
  • I took a look and did the "Copy as curl" stuff. I ran it in the command line, but a 401 for not having token raised – madtyn Nov 08 '20 at 19:34
  • 1
    @madtyn I updated my answer ;) For "Copy as curl" for me it's working if I using it directly. I'm using Firefox and as far as i know this is not generating exact same curl output so give it a try maybe – Calumah Nov 09 '20 at 08:11
  • Thanks. Given that this is a personal project, I have to wait until I finish working. I will take a look then – madtyn Nov 09 '20 at 11:03
  • 1
    I'm accepting the answer, but the Authorization bearer expires, so I must loook for another solution. Thanks anyway. :-) – madtyn Nov 14 '20 at 09:48