在Python中使用googlepicasaapi

2024-09-30 14:25:43 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经成功地使用了谷歌的PicasaAPI从过去的6个月左右。 今天我开始出错了

    raise GooglePhotosException(e.args[0])
GooglePhotosException: (403, 'Forbidden', 'Authorization required')

我检查了我的证件。在

^{pr2}$

从昨天开始一切都很顺利。有人有线索吗?在

编辑

关于python的Picasa的示例也不起作用。 URL

#!/usr/bin/python2.5

import gdata.photos.service
import gdata.media
import gdata.geo

gd_client = gdata.photos.service.PhotosService()
gd_client.email = '=change='     # Set your Picasaweb e-mail address...
gd_client.password = '=change='  # ... and password
gd_client.source = 'api-sample-google-com'
gd_client.ProgrammaticLogin()

albums = gd_client.GetUserFeed()
for album in albums.entry:
  print 'Album: %s (%s)' % (album.title.text, album.numphotos.text)

  photos = gd_client.GetFeed('/data/feed/api/user/default/albumid/%s?kind=photo' % (album.gphoto_id.text))
  for photo in photos.entry:
    print '  Photo:', photo.title.text

    tags = gd_client.GetFeed('/data/feed/api/user/default/albumid/%s/photoid/%s?kind=tag' % (album.gphoto_id.text, photo.gphoto_id.text))
    for tag in tags.entry:
      print '    Tag:', tag.title.text

    comments = gd_client.GetFeed('/data/feed/api/user/default/albumid/%s/photoid/%s?kind=comment' % (album.gphoto_id.text, photo.gphoto_id.text))
    for comment in comments.entry:
      print '    Comment:', comment.content.text

编辑2

完全回溯

Traceback (most recent call last):
  File "/Users/mac/Picasa_API.py", line 158, in <module>
    check_api()
  File "/Users/mac/Picasa_API.py", line 140, in check_api
    albums = gd_client.GetUserFeed()
  File "/Users/mac/destipak/env/lib/python2.7/site-packages/gdata/photos/service.py", line 235, in GetUserFeed
    return self.GetFeed(uri, limit=limit)
  File "/Users/mac/destipak/env/lib/python2.7/site-packages/gdata/photos/service.py", line 180, in GetFeed
    raise GooglePhotosException(e.args[0])
gdata.photos.service.GooglePhotosException: (403, 'Forbidden', 'Authorization required')

Tags: textinclientapiidforalbumservice
2条回答

我也碰到过这个。电子邮件/密码验证似乎已关闭,我们需要切换到OAuth2。看到了吗 https://groups.google.com/forum/#!topic/Google-Picasa-Data-API/4meiAJ40l3E

下面是我用来让OAuth2身份验证与Picasa一起工作的代码。首先,您需要通过googledeveloper控制台在https://console.developers.google.com/创建一个客户机ID,然后必须将客户机机密作为JSON下载并将文件名传递给OAuth2Login。在

第一次运行此代码时,必须通过web浏览器对客户端进行授权,并将获得的代码粘贴到应用程序中。然后,凭证存储在凭证存储区指定的文件中。在

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

    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

相关问题 更多 >