Open1

Google Play Developer APIのpurchases.subscriptionsv2.getで401 Unauthorizedのエラー

たっくんたっくん

エラーの内容

purchases.subscriptionsv2.getをラップしているgoogle-apis-androidpublisher_v3のgemでsubscriptionを取得しようとすると以下のエラー

The current user has insufficient permissions to perform the requested operation.

エラーの詳細

#<HTTP::Message:0x00000002b154f1d8 @http_body=#<HTTP::Message::Body:0x00000002b154f138 @body="{\n" + "  \"error\": {\n" + "    \"code\": 401,\n" + "    \"message\": \"The current user has insufficient permissions to perform the requested operation.\",\n" + "    \"errors\": [\n" + "      {\n" + "        \"message\": \"The current user has insufficient permissions to perform the requested operation.\",\n" + "        \"domain\": \"androidpublisher\",\n" + "        \"reason\": \"permissionDenied\"\n" + "      }\n" + "    ]\n" + "  }\n" + "}\n", @chunk_size=nil, @positions=nil, @size=0>, @http_header=#<HTTP::Message::Headers:0x00000002b154f1b0 @body_charset=nil, @body_date=nil, @body_encoding=#<Encoding:UTF-8>, @body_size=0, @body_type=nil, @chunked=false, @dumped=false, @header_item=[["Vary", "Origin"], ["Vary", "X-Origin"], ["Vary", "Referer"], ["Content-Type", "application/json; charset=UTF-8"], ["Content-Encoding", "gzip"], ["Date", "Tue, 12 Mar 2024 05:05:22 GMT"], ["Server", "ESF"], ["Cache-Control", "private"], ["X-XSS-Protection", "0"], ["X-Frame-Options", "SAMEORIGIN"], ["X-Content-Type-Options", "nosniff"], ["Alt-Svc", "h3=\":443\"; ma=2592000,h3-29=\":443\"; ma=2592000"], ["Transfer-Encoding", "chunked"]], @http_version="1.1", @is_request=false, @reason_phrase="Unauthorized", @request_absolute_uri=nil, @request_method="GET", @request_query=nil, @request_uri=#<Addressable::URI:0x10e140 URI:https://androidpublisher.googleapis.com/androidpublisher/v3/applications/com.Flamers.Memoria/purchases/subscriptionsv2/tokens/xxx>, @status_code=401>, @peer_cert=#<OpenSSL::X509::Certificate subject=#<OpenSSL::X509::Name CN=upload.video.google.com>, issuer=#<OpenSSL::X509::Name CN=GTS CA 1C3,O=Google Trust Services LLC,C=US>, serial=#<OpenSSL::BN 168175146508564667403645459986015235053>, not_before=2024-02-19 08:18:43 UTC, not_after=2024-05-13 08:18:42 UTC>, @previous=nil>
Caught error Unauthorized

コード

gem 'google-apis-androidpublisher_v3'
client = Google::Apis::AndroidpublisherV3::AndroidPublisherService.new.tap do |client|
  client.authorization = Google::Auth::ServiceAccountCredentials.make_creds(
    # 動作確認のためにサービスアカウントの鍵をローカルに置いている
    json_key_io: Rails.root.join('service-account.json'),
    scope: 'https://www.googleapis.com/auth/androidpublisher'
  )
end
client.get_purchase_subscriptionsv2(package_name, purchase_token)

新規で作成したGoogle Cloudのプロジェクトでは問題は起こらなくて、既存のプロジェクトにReal-time Developer Notificationsを導入しようとすると問題が起こった。

確認したこと

以下を確認しても問題なかったので困った

解決策

Google Cloudのバグなのか、設定が伝播するのに時間がかかるからなのかは不明だが、Google Play Console > 商品 > 定期購入 を適当に編集、変更を保存、編集した内容を元に戻す、再度変更を保存で直った。

参考
https://stackoverflow.com/questions/43536904/google-play-developer-api-the-current-user-has-insufficient-permissions-to-pe