尝试通过Google Picasa API添加照片或相册时无法进行身份验证

时间:2016-04-08 21:07:32

标签: python api authentication photo picasa

我正在尝试通过Picasa的API将照片上传到相册,但是我收到了身份验证错误。

我的凭据在程序启动时正在成功验证,而不是在我尝试上传照片或添加相册时

这是我的代码:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>


<script>
  $(document).ready(function() {

    var docWidth = $(document).width();
    var bottomWidth = $("bottom").width();

    alert(docWidth);

    $("bottom").css("width", docWidth);

  });
</script>

<script>
  var test = $('.bottom').val();
  alert(test)
</script>


<html>

<head>
</head>

<body>

  <div class="bottom">

  </div>



</body>

</html>

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

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

这是我一直关注的指南:https://developers.google.com/picasa-web/docs/1.0/developers_guide_python#request-a-list-of-albums

1 个答案:

答案 0 :(得分:1)

您是否真的对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