2015-05-29 42 views
1

Google最近轉移到了OAuth2.0,我們需要更改之前的身份驗證方式(即從ProgrammaticLoginOAuth2.0)。Python - 使用Picasa創建相冊錯誤

我可以成功訪問相冊和讀取照片數據/評論。當我嘗試添加新相冊/照片或嘗試寫入數據時,出現以下錯誤。

client = PhotosService(email="xxxx")  
    ... 
    ... 
    ... 
    #After successfull OAuth 
    album = client.InsertAlbum(title="Temp album", summary="My summary", access="public") 

此行會導致以下錯誤。

File "/Users/mac/destipak/env/lib/python2.7/site-packages/gdata/photos/service.py", line 358, in InsertAlbum 
    raise GooglePhotosException(e.args[0]) 
gdata.photos.service.GooglePhotosException: (403, 'Forbidden', 'Modification only allowed with api authentication.') 

回答

0

我不太確定,但是您是否實際上對OAuth2進行了更改?我用下面的代碼,它的工作。

def OAuth2Login(client_secrets, credential_store, email): 
scope='https://picasaweb.google.com/data/' 
user_agent='testingApp' 

storage = Storage(credential_store) 
credentials = storage.get() 
if credentials is None or credentials.invalid: 
    flow = flow_from_clientsecrets(client_secrets, scope=scope, redirect_uri='urn:ietf:wg:oauth:2.0:oob') 
    uri = flow.step1_get_authorize_url() 
    webbrowser.open(uri) 
    code = raw_input('Enter the authentication code: ').strip() 
    credentials = flow.step2_exchange(code) 
    storage.put(credentials) 

if (credentials.token_expiry - datetime.utcnow()) < timedelta(minutes=5): 
    http = httplib2.Http() 
    http = credentials.authorize(http) 
    credentials.refresh(http) 

gd_client = gdata.photos.service.PhotosService(source=user_agent, 
              email=email, 
              additional_headers={'Authorization' : 'Bearer %s' % credentials.access_token}) 

return gd_client 
album = gd_client.InsertAlbum('test', 'My Test Album', access='protected') 

我確實有建立在谷歌開發者門戶網站的API密鑰和下載json的祕密,但這樣做,我是能夠成功地創建相冊後。這個回購非常有幫助https://github.com/MicOestergaard/picasawebuploader