Python - 使用Picasa错误创建相册

时间:2015-05-29 06:34:31

标签: python oauth-2.0 picasa

Google最近转向OAuth2.0,我们需要更改以前的身份验证机制(即从ProgrammaticLogin更改为OAuth2.0)。

我可以成功访问相册并阅读照片上的数据/评论。当我尝试添加新相册/照片或尝试写入数据时,我收到以下错误。

    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.')

1 个答案:

答案 0 :(得分: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')

我必须在Google开发人员门户网站中创建一个API密钥并下载json秘密,但在这之后我能够成功创建一个相册。这个回购非常有帮助https://github.com/MicOestergaard/picasawebuploader