Skip to content

Instantly share code, notes, and snippets.

@tdtds
Last active August 29, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tdtds/4cbf0fef8feddd9a121e to your computer and use it in GitHub Desktop.
Save tdtds/4cbf0fef8feddd9a121e to your computer and use it in GitHub Desktop.
PicasaWeb APIのOAuthがうまく使えないメモ

要件

  • 従来パスワード認証で使っていたpicasa gemを使いたい(OAuth対応してると書いてある)
  • アルバム作成、指定アルバムへの画像追加がしたい

やったこと

  • Google Developers Consoleでプロジェクトをひとつ作成
  • そのプロジェクトの「認証情報」でサービスアカウントを追加、ここでメールアドレス「hoge@developer.gserviceaccount.com」を得る
  • P12キーを生成してダウンロード(test.p12)
  • picasa gemに加えてgoogle-api-client gemをインストール
  • (以下はgoogle-api-clientとpicasaをrequireしたpry上で作業)
  • クライアントとキーを生成
client = Google::APIClient.new(:application_name => '')
key = ': key = Google::APIClient::KeyUtils.load_from_pkcs12('test.p12', 'notasecret')
  • クライアントに認証情報をセット
client.authorization = Signet::OAuth2::Client.new(
  :token_credential_uri => "https://accounts.google.com/o/oauth2/token", 
  :audience             => "https://accounts.google.com/o/oauth2/token", 
  :scope                => "https://picasaweb.google.com/data/",
  :issuer               => "hoge@developer.gserviceaccount.com",
  :signing_key          => key
)
  • アクセストークンを取得
client.authorization.fetch_access_token!
=> {"access_token"=>'foobarbaz....',
    "token_type"=>""Bearer"",
	 "expires_in"=>3600}
  • picasaクライアントを作成。user_idには自分のアカウントのものを使用
picasa = Picasa::Client.new(user_id: 'fuga@gmail.com', authorization_header: "Bearer #{client.authorization.access_token}") 
  • Picasa上にアルバムを作成しようとする……が失敗
picasa.album.create(title: 'api test', timestamp: Time.now.to_i)
=> Picasa::ForbiddenError: Not authorized to modify this resource.

ちなみにuser_idに他のものを指定したりするとUser unknownになるので、使い方は間違ってないように思う。メッセージからしても認証には成功してるけど(権限がなくて)認可に失敗しているように読み取れるし。

とりあえずここまで。

参考資料

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment