2011年1月28日金曜日

picasawebにも画像を投稿してみる。 はてなブックマークに追加

Google Baseを試す必要があったので、勉強代わりにPicasa Web Albums Data APIに画像をアップしてみた。

認証ができているかどうかのテストなので適当です。アップ時にメタ情報も登録できるけど、XMLと画像を同時アップが結構手間なのではしょっちゃいました。

  1. 事前に picasawebでアップ用のアルバムの作成と、API Keyの取得が必要です。
  2. albumidは、アルバム情報取得APIできますが、面倒でしたらアルバムページのRSSのURLから抽出できます。
  3.  gbaseのpythonライブラリは、今回は調査がメインだったので使っていません。


#!/usr/bin/python
# -*- coding: utf-8 -*-
import urllib
import urllib2

#http://code.google.com/intl/ja/apis/accounts/docs/AuthForInstalledApps.html
#http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/developers_guide_protocol.html#PostPhotos
#コマンドラインからの認証
url = 'https://www.google.com/accounts/ClientLogin'
#picasaweb の service は lh2 それ以外は適宜
params = urllib.urlencode({'Email':"YOUR_MAIL_ADDRESS", 'Passwd':"YOUR_PASSWD","service":"lh2","accountType":"HOSTED_OR_GOOGLE","source":"YOUR_APP_NAME_AND_VERSION"})
f = urllib.urlopen(url, params)
r = f.read().split("\n")[2]
#responce SID,LSIDは不要,Authのみ取得する
Auth = r.split("=")[1]

#画像を開く
i = open("/Volumes/hd1/Users/nishiokya/Pictures/shoebill.jpg","r");
img = i.read()

#user と albumidが必要
url = 'https://picasaweb.google.com/data/feed/api/user/nishiokya/albumid/5304381279593343265'
req = urllib2.Request(url)
req.add_header("Content-Type"," image/jpeg")
req.add_header("Content-Length",len(img))
req.add_header("Slug","shoebill.jpg")
req.add_header("Authorization","GoogleLogin  auth="+Auth)
#google no appid 
#http://code.google.com/intl/ja/apis/base/signup.html
#Installed applications にチェック
req.add_header("key","ABQIAAAAp2K6l3b04u-NrzcaeIej-BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxXXXXXXXXXXXXXXXXXXXX")
req.add_data(img )

try:
   resp = urllib2.urlopen(req)
   print resp.code 
   print resp.info()
   print resp.read()
except urllib2.HTTPError, e:
   print "Error: %s" % e
   print e.read()