- 従来パスワード認証で使っていたpicasa gemを使いたい(OAuth対応してると書いてある)
- アルバム作成、指定アルバムへの画像追加がしたい
- Google Developers Consoleでプロジェクトをひとつ作成
- そのプロジェクトの「認証情報」でサービスアカウントを追加、ここでメールアドレス「[email protected]」を得る
- 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 => "[email protected]",
: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: '[email protected]', 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になるので、使い方は間違ってないように思う。メッセージからしても認証には成功してるけど(権限がなくて)認可に失敗しているように読み取れるし。
とりあえずここまで。