尝试通过Google Picasa API添加照片或相册时无法进行身份验证 [英] Unable to authenticate when trying to add photos or albums through Google Picasa API

查看:141
本文介绍了尝试通过Google Picasa API添加照片或相册时无法进行身份验证的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过Picasa的API将照片上传到相册,但是出现验证错误.

程序启动时,我的凭据就已成功通过身份验证,而当我尝试上传照片或添加相册时,并非如此

这是我的代码:

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

gd_client = gdata.photos.service.PhotosService()
gd_client.email = 'username@gmail.com'
gd_client.password = 'password'
gd_client.source = 'exampleCo-exampleApp-1'
gd_client.ProgrammaticLogin()

username = 'username'
albums = gd_client.GetUserFeed(user=username)
album_id = albums.entry[0].gphoto_id.text
album_url = '/data/feed/api/user/%s/albumid/%s' % (username, album_id)
path = 'C:\Users\Public\Pictures\Sample Pictures\Desert.jpg'

album = gd_client.InsertAlbum(title='New album', summary='This is an album')

photo = gd_client.InsertPhotoSimple(album_url, 'New Photo', 
    'Uploaded using the API', path, content_type='image/jpeg')

这是我尝试添加相册或上传照片时收到的错误:

Traceback (most recent call last):
  File "C:/Users/1020071/Documents/test.py", line 19, in <module>
    album = gd_client.InsertAlbum(title='New album', summary='This is an album')
  File "C:\Python27\lib\site-packages\gdata\photos\service.py", line 358, in InsertAlbum
    raise GooglePhotosException(e.args[0])
GooglePhotosException: (403, 'Forbidden', 'Modification only allowed with api authentication.')

这是我一直遵循的指南:解决方案

您实际上是通过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#authentication .. >

I am trying to upload a photo to an album through Picasa's API, however I am getting an authentication error.

My credentials are being successfully authenticated right when the program starts, just not when I am trying to upload a photo or add an album

Here is the code I have:

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

gd_client = gdata.photos.service.PhotosService()
gd_client.email = 'username@gmail.com'
gd_client.password = 'password'
gd_client.source = 'exampleCo-exampleApp-1'
gd_client.ProgrammaticLogin()

username = 'username'
albums = gd_client.GetUserFeed(user=username)
album_id = albums.entry[0].gphoto_id.text
album_url = '/data/feed/api/user/%s/albumid/%s' % (username, album_id)
path = 'C:\Users\Public\Pictures\Sample Pictures\Desert.jpg'

album = gd_client.InsertAlbum(title='New album', summary='This is an album')

photo = gd_client.InsertPhotoSimple(album_url, 'New Photo', 
    'Uploaded using the API', path, content_type='image/jpeg')

Here is the error I am receiving when I try to add an album or upload a photo:

Traceback (most recent call last):
  File "C:/Users/1020071/Documents/test.py", line 19, in <module>
    album = gd_client.InsertAlbum(title='New album', summary='This is an album')
  File "C:\Python27\lib\site-packages\gdata\photos\service.py", line 358, in InsertAlbum
    raise GooglePhotosException(e.args[0])
GooglePhotosException: (403, 'Forbidden', 'Modification only allowed with api authentication.')

This is the guide I have been following: https://developers.google.com/picasa-web/docs/1.0/developers_guide_python#request-a-list-of-albums

解决方案

Did you actually make the change over OAuth2? The following code should work:

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

You should create an API Key in the Google developer portal and download the json secret. This repo was very helpful https://github.com/MicOestergaard/picasawebuploader#authentication.

这篇关于尝试通过Google Picasa API添加照片或相册时无法进行身份验证的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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