8

我有一個基於google-api-python-client的小應用程序,但批處理請求在幾天內不再工作(錯誤404)。谷歌API批處理請求通過Python客戶端返回每個呼叫的HttpError 404

例如,下面的代碼工作得很好,直到幾天前。

from apiclient.http import BatchHttpRequest 
from apiclient.discovery import build 
import json 

DEVELOPER_KEY = "foobar" 
YOUTUBE_API_SERVICE_NAME = "youtube" 
YOUTUBE_API_VERSION = "v3" 

channelIds = ['UC2C_jShtL725hvbm1arSV9w','UC2C_jShtL725hvbm1arSV9w'] 

parts_list = [ 
"id", 
"brandingSettings", 
] 

fields_list = [ 
"items/id", 
"items/brandingSettings/channel",  
] 

parts = ",".join(parts_list) 
fields = ",".join(fields_list) 

request_map = {} 

def this_is_the_callback_function(request_id, response, exception): 
    if exception is not None: 
    # Do something with the exception 
    print exception 
    pass 
    else: 
    print request_id 
    print request_map[int(request_id)] 
    print json.dumps(response,sort_keys=True, indent=4) 

service = build(YOUTUBE_API_SERVICE_NAME, YOUTUBE_API_VERSION, developerKey=DEVELOPER_KEY) 
batch = service.new_batch_http_request(callback=this_is_the_callback_function) 

channels = service.channels() 
i = 0 
for c in channelIds: 
    i += 1 
    request_map[i] = c 
    request = channels.list(id=c,part=parts, fields=fields) 
    batch.add(request) 
print request_map 
batch.execute() 

現在,如果我運行它,我得到:

{1: 'UC2C_jShtL725hvbm1arSV9w', 2: 'UC2C_jShtL725hvbm1arSV9w'} 
<HttpError 404 when requesting https://www.googleapis.com/youtube/v3/channels?fields=items%2Fid%2Citems%2FbrandingSettings%2Fchannel&alt=json&part=id%2CbrandingSettings&id=UC2C_jShtL725hvbm1arSV9w&key=foobar returned "Not Found"> 
<HttpError 404 when requesting https://www.googleapis.com/youtube/v3/channels?fields=items%2Fid%2Citems%2FbrandingSettings%2Fchannel&alt=json&part=id%2CbrandingSettings&id=UC2C_jShtL725hvbm1arSV9w&key=foobar returned "Not Found"> 

奇怪。我覺得奇怪的是,如果我試圖向這些鏈接發出簡單的請求(簡單地通過在瀏覽器中剪切並粘貼URL),服務器將像往常一樣返回數據。

+0

您是否得到了此答案或找到解決方法?我有完全相同的問題。 – spuriousdata

+0

你好@spuriousdata。目前沒有答案,沒有解決方法:(你有一個關於爲什麼發生的想法? – floatingpurr

+1

我不知道,但我已經聯繫谷歌,他們似乎正在努力修復它 – spuriousdata

回答

0

看來這個問題是用最新版本的python庫修復的。然而,如果有人仍然需要解決它,或者正在使用不同的環境(如iOS或Android),那就是解決方案。

似乎谷歌單個批次的URL,這是之前已經使用:

https://www.googleapis.com/batch

這個工作做好我的應用程序,直到它突然停了下來。看來,解決辦法是改變URL批次呼叫:

https://www.googleapis.com/batch/youtube/v3 (對YouTube的API)

通常是谷歌API對象將公開屬性,允許更改此值,但如果沒有它可以從源頭上改變。

+0

我相信問題已經解決了服務器端。這不是Python庫的問題。 – floatingpurr

相關問題