使用谷歌的Picasa API使用Python [英] Using Google Picasa API with Python

查看:286
本文介绍了使用谷歌的Picasa API使用Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直使用谷歌的Picasa API成功地从过去的6个月左右。
今天,我已经开始收到错误

 提高GooglePhotosException(e.args [0])
GooglePhotosException:(403,'禁止','需要授权)

我检查了我的凭据。

  self.gd_client = gdata.photos.service.PhotosService()
    self.gd_client.email = EmailOfTheUploadingPictureAccount
    self.gd_client.password = PasswordOfTheAccount
    self.gd_client.source ='destipak'#Not能肯定
    self.feed_url =/数据/料/ API /用户/
    self.entry_url =/数据/进入/ API /用户/
    self.gd_client.ProgrammaticLogin()

一切都从昨天开始运作良好。任何人有任何线索?

修改

在示例的Picasa 给出的蟒蛇也不能正常工作。
网址

 #!的/ usr / bin中/的python2.5进口gdata.photos.service
进口gdata.media
进口gdata.geogd_client = gdata.photos.service.PhotosService()
gd_client.email ='=变化='#设置您的Picasa网络相册的e-mail地址...
gd_client.password ='​​=变化='#...和密码
gd_client.source ='API的样本谷歌-COM
gd_client.ProgrammaticLogin()专辑= gd_client.GetUserFeed()
在albums.entry专辑:
  打印专辑:%S(%S)'%(album.title.text,album.numphotos.text)  照片= gd_client.GetFeed('/数据/料/ API /用户/默认/ ALBUMID /%s吗?那种照片=%(album.gphoto_id.text))
  对于照片photos.entry:
    打印图片:',photo.title.text    标签= gd_client.GetFeed('/数据/料/ API /用户/默认/ ALBUMID /%S / PHOTOID /%s吗?那种=标签'%(album.gphoto_id.text,photo.gphoto_id.text))
    在tags.entry标签:
      打印标签:,tag.title.text    评论= gd_client.GetFeed('/数据/料/ API /用户/默认/ ALBUMID /%S / PHOTOID /%s吗?那种=注释'%(album.gphoto_id.text,photo.gphoto_id.text))
    在comments.entry评论:
      打印评论:,comment.content.text

编辑2

全部追踪

 回溯(最后最近一次调用):
  文件/Users/mac/Picasa_API.py,158线,上述<&模块GT;
    check_api()
  文件/Users/mac/Picasa_API.py,140线,在check_api
    专辑= gd_client.GetUserFeed()
  文件/Users/mac/destipak/env/lib/python2.7/site-packages/gdata/photos/service.py,235线,在GetUserFeed
    返回self.GetFeed(URI,限值为限)
  文件/Users/mac/destipak/env/lib/python2.7/site-packages/gdata/photos/service.py,180线,在GetFeed
    提高GooglePhotosException(e.args [0])
gdata.photos.service.GooglePhotosException:(403,'禁止','授权要求)


解决方案

下面是code我用得到的OAuth2验证与Picasa的工作。首先,你需要创建通过谷歌开发者控制台的客户端ID:从 https://console.developers.google.com / 然后您必须下载客户端秘密为JSON和文件名传递给OAuth2Login。

在运行此code中的第一次,你将不得不通过Web浏览器授权客户端,并粘贴code你到达那里到应用程序。凭据然后存储由credential_store指定的文件中。

 高清OAuth2Login(CLIENT_SECRETS,credential_store,电子邮件):
    范围=的https://picasaweb.google.com/data/
    USER_AGENT ='的myapp    存储=存储(credential_store)
    凭据= storage.get()
    如果凭据是无或credentials.invalid:
        流量= flow_from_clientsecrets(CLIENT_SECRETS,范围=范围,REDIRECT_URI ='金塔:IETF:WG:OAuth的:2.0:OOB)
        URI = flow.step1_get_authorize_url()
        webbrowser.open(URI)
        code的raw_input =('输入验证code:').strip()
        凭据= flow.step2_exchange(code)
        storage.put(凭证)    如果(credentials.token_expiry - datetime.utcnow())≤; timedelta(分钟= 5):
        HTTP =的httplib2.Http()
        HTTP = credentials.authorize(http)的
        credentials.refresh(http)的    gd_client = gdata.photos.service.PhotosService(来源= USER_AGENT,
                                               电子邮件=电子邮件,
                                               additional_headers = {'授权':'承载%s'的%credentials.access_token})    回报gd_client

I have been using Google's Picasa API successfully from last 6 months or so. Today I have started getting an error

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

I checked my credentials.

    self.gd_client = gdata.photos.service.PhotosService()
    self.gd_client.email = EmailOfTheUploadingPictureAccount
    self.gd_client.password = PasswordOfTheAccount
    self.gd_client.source = 'destipak' #Not sure about that
    self.feed_url = "/data/feed/api/user/"
    self.entry_url = "/data/entry/api/user/"
    self.gd_client.ProgrammaticLogin() 

Everything was working well since yesterday. Anyone has any clues?

EDIT

Example given on Picasa for python is also not working. 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

EDIT 2

Full traceback

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

解决方案

Here is the code I use to get OAuth2 authentication working with Picasa. First you need to create a client ID through the Google Developer Console: at https://console.developers.google.com/ and then you must download the client secrets as JSON and pass the filename to OAuth2Login.

The first time you run this code, you will have to authorize the client through your web browser, and paste the code you get there into the application. The credentials are then stored in the file specified by credential_store.

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

这篇关于使用谷歌的Picasa API使用Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆