zzzzz838'sdiary

zzzzz838の日記

調べたことを忘れないように書いています。

GoogleのOAuth2でinvalid_grantエラーが表示される場合

Androidのアプリ内課金の有効期限をチェックのため、PHPでサーバーを作っています。

これはアプリから期限を問い合わせできないため、必要となります。

問い合わせのためにGoogle Play Android Developer APIを使います。

その際、OAuth2.0を使った認証を行いますが、invalid_grantというエラーが発生しています。

解決法

いろいろ調べた結果、stackoverflowに解決法が載っていました。

invalid_grant trying to get oAuth token from google - Stack Overflow

>In essence, when you add an OAuth2 Client in your Google API's console Google will give you a "Client ID" and an "Email address" (assuming you select "webapp" as your client type). And despite Google's misleading naming conventions, they expect you to send the "Email address" as the value of the client_id parameter when you access their OAuth2 API's.

基本的に、Google APIのコンソールにOAuth2クライアントを追加すると、Googleは「クライアントID」と「メールアドレス」を提供します(クライアントタイプとして「webapp」を選択した場合)。また、Googleの誤解を招く命名規則にも関わらず、OAuth2 APIにアクセスするときに、client_idパラメータの値として「メールアドレス」を送信することを期待しています。

 

「EmailAddressをclient_idの値として送信すればいい」と書いてあるけど、

このままではうまく行かなくて、EmailAddressはEmailAddress、client_idはclient_idとして送るとうまくいった。

 code、client_id、client_secret、email address、redirect_uri、grant_typeの6つの値が必要となる。