2011年1月28日金曜日

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

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

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

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


  1. #!/usr/bin/python  
  2. # -*- coding: utf-8 -*-  
  3. import urllib  
  4. import urllib2  
  5.   
  6. #http://code.google.com/intl/ja/apis/accounts/docs/AuthForInstalledApps.html  
  7. #http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/developers_guide_protocol.html#PostPhotos  
  8. #コマンドラインからの認証  
  9. url = 'https://www.google.com/accounts/ClientLogin'  
  10. #picasaweb の service は lh2 それ以外は適宜  
  11. params = urllib.urlencode({'Email':"YOUR_MAIL_ADDRESS"'Passwd':"YOUR_PASSWD","service":"lh2","accountType":"HOSTED_OR_GOOGLE","source":"YOUR_APP_NAME_AND_VERSION"})  
  12. f = urllib.urlopen(url, params)  
  13. r = f.read().split("\n")[2]  
  14. #responce SID,LSIDは不要,Authのみ取得する  
  15. Auth = r.split("=")[1]  
  16.   
  17. #画像を開く  
  18. i = open("/Volumes/hd1/Users/nishiokya/Pictures/shoebill.jpg","r");  
  19. img = i.read()  
  20.   
  21. #user と albumidが必要  
  22. url = 'https://picasaweb.google.com/data/feed/api/user/nishiokya/albumid/5304381279593343265'  
  23. req = urllib2.Request(url)  
  24. req.add_header("Content-Type"," image/jpeg")  
  25. req.add_header("Content-Length",len(img))  
  26. req.add_header("Slug","shoebill.jpg")  
  27. req.add_header("Authorization","GoogleLogin  auth="+Auth)  
  28. #google no appid   
  29. #http://code.google.com/intl/ja/apis/base/signup.html  
  30. #Installed applications にチェック  
  31. req.add_header("key","ABQIAAAAp2K6l3b04u-NrzcaeIej-BT2yXp_ZAY8_ufC3CFXhHIE1NvwkxXXXXXXXXXXXXXXXXXXXX")  
  32. req.add_data(img )  
  33.   
  34. try:  
  35.    resp = urllib2.urlopen(req)  
  36.    print resp.code   
  37.    print resp.info()  
  38.    print resp.read()  
  39. except urllib2.HTTPError, e:  
  40.    print "Error: %s" % e  
  41.    print e.read()  

0 件のコメント: