概要
Google Playに公開したアプリへのユーザーからのレビューをコードから取得してみたのでメモです。
準備
サービスアカウントの作成
Google Cloud Platformのコンソールの認証情報からサービスアカウントを作成し、client_secret.json
をダウンロードしておきます。
Google Play Developerコンソールとの紐付け
Google Play Developerコンソールの[設定] -> [APIアクセス]で作成したサービスアカウントを紐付けます。また、サービスアカウントを作成したプロジェクトでGoogle Play Android Developer APIを有効化しておきます。
API Clientの利用
今回、APIへのアクセスはGoogle API Client Library for Rubyを使用します。
API Clientからの認証処理
client_secret.json
を使用して認証処理を実行します。認証処理のコードは以下の様になります。scopeにはhttps://www.googleapis.com/auth/androidpublisher
を指定します。
require 'googleauth'
options = JSON.parse(File.read('client_secret.json'))
key = OpenSSL::PKey::RSA.new(options['private_key'])
auth = Signet::OAuth2::Client.new(
token_credential_uri: options['token_uri'],
audience: options['token_uri'],
scope: %w(
https://www.googleapis.com/auth/androidpublisher
),
issuer: options['client_email'],
signing_key: key
)
レビューリストの取得
Google Play Developer APIのドキュメントを確認するとレビューを取得できることがわかります。さらに、google-api-ruby-clientのdocsを見てみると、Google::Apis::AndroidpublisherV2を使用することでレビューを取得できそうです。
Google::Apis::AndroidpublisherV2::AndroidPublisherService.new
でsearviceを作成し、service.authorization
に認証情報を入れます。そして、service.list_reviews
にGoogle Playで公開しているアプリのpackage名を引数に指定することでレビューのリストを取得できます。
service = Google::Apis::AndroidpublisherV2::AndroidPublisherService.new
service.client_options.application_name = 'Google Play Developer Console Sample'
service.authorization = authorize
response = service.list_reviews('YOUR_PACKAGE_NAME')
最終的なコードは以下の通りです。
require 'google/apis/androidpublisher_v2'
require 'googleauth'
require 'pp'
def authorize
options = JSON.parse(File.read('client_secret.json'))
key = OpenSSL::PKey::RSA.new(options['private_key'])
auth = Signet::OAuth2::Client.new(
token_credential_uri: options['token_uri'],
audience: options['token_uri'],
scope: %w(
https://www.googleapis.com/auth/androidpublisher
),
issuer: options['client_email'],
signing_key: key
)
auth
end
service = Google::Apis::AndroidpublisherV2::AndroidPublisherService.new
service.client_options.application_name = 'Google Play Developer Console Sample'
service.authorization = authorize
response = service.list_reviews('YOUR_PACKAGE_NAME')
pp response
参考
- https://developers.google.com/android-publisher/getting_started
- https://developers.google.com/android-publisher/reply-to-reviews
- https://developers.google.com/android-publisher/authorization
- http://www.rubydoc.info/github/google/google-api-ruby-client/Google/Apis/AndroidpublisherV2
- https://developers.google.com/apis-explorer/#search/play/androidpublisher/v2/androidpublisher.reviews.list