LoginSignup
18
16

More than 5 years have passed since last update.

Google Playへ公開したアプリのレビューをコードから取得する

Last updated at Posted at 2016-10-21

概要

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

参考

18
16
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
18
16