我尝试使用Picasa API与AuthSub。我的代码:
GD_CLIENT = gdata.photos.service.PhotosService()
def login(request):
def GetAuthSubUrl():
callback = 'http://127.0.0.1:8000/callback'
scope = 'https://picasaweb.google.com/data/'
secure = False
session = True
return GD_CLIENT.GenerateAuthSubURL(callback, scope, secure, session)
auth_sub_url = GetAuthSubUrl()
return HttpResponseRedirect(auth_sub_url)
def confirm(request):
authsub_token = request.GET['token']
token = GD_CLIENT.SetAuthSubToken(authsub_token)
GD_CLIENT.UpgradeToSessionToken()
GD_CLIENT.auth_token = token
return direct_to_template(request, 'base.djhtml')
def add_album(request):
form = AddAlbum(request.POST or None)
if form.is_valid():
data = form.cleaned_data
title = data.get('title')
summary = data.get('summary')
GD_CLIENT.InsertAlbum(title=title, summary=summary)
return HttpResponseRedirect('/get_albums/')
return render(request, 'add_form.djhtml', {'form': form})
我在add_album:
得到一个错误(404, 'Not Found', 'Unknown user.')
回溯:文件"/home/i159/Envs/photorulez/lib/python2.6/site-packages/django/core/handlers/base.py111. Response = callback(request, *callback_args, **callback_kwargs)文件"/home/i159/workspace/photoulez/photoulez/photoapp/views.py49. GD_CLIENT。InsertAlbum(标题、总结= =总结)文件"/home/i159/Envs/photorulez/lib/python2.6/site-packages/gdata/photos/service.py358. 提高GooglePhotosException (e.args [0])
异常类型:GooglePhotosException at/add_album/异常值:(404,'Not Found', 'Unknown user.')
为什么要抬高?需要做哪些改变?
我明白了!GD_CLIENT = gdata.photos.service.PhotosService()需要email
关键字参数作为Google帐户用户名。
gdata.photos.sevice.py
class PhotosService(gdata.service.GDataService):
ssl = True
userUri = '/data/feed/api/user/%s'
def __init__(self, email=None, password=None, source=None,
server='picasaweb.google.com', additional_headers=None,
**kwargs):
所以应该是:
GD_CLIENT = gdata.photos.service.PhotosService(email='username')
这是我验证用户是否有会话令牌的函数:
def get_client(authsub_token):
gd_client = gdata.photos.service.PhotosService(email='default')
gd_client.SetAuthSubToken(authsub_token)
return gd_client
如果您将电子邮件或用户名设置为"默认",他将使用授权令牌的用户