【Google pay接入】获取refresh_token

开发相关导入包及配置这里就不多说,只针对GP后台操作做个记录

1、先打开Google Play Console后台,在API access中创建或绑定 Google Cloud Platform中的项目,这边是创建,创建之后点击“View project”可直接跳转至Google Cloud Platform后台
gp console后台.png
2、在Google Cloud Platform后台打开APIs and Services
image.png
3、开启OAuth consent screen
image.png
4、填写相关信息
image.png
image.png
5、到第二步“Scopes”先直接跳过
image.png
6、第三步“Test users”可添加测试人员账号
image.png
7、最后一步直接返回,至此完成了OAuth consent screen的创建
image.png
8、创建web client
image.png
image.png
image.png
9、创建成功后获得Client ID 和Client Secret
image.png
10、在首页也能看到创建好的client
image.png
11、返回Google Play Console控制后台可以看到此项目
image.png

至此,相关的设置已经完成,接下来拿到项目的Client ID 和Client Secret后就可以来获取code,通过code去获取refresh_token

12、获取code,在浏览器中输入以下链接

 

https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri={凭据页签设置的重定向地址}&client_id={创建的凭据的clientId}

完整效果:
https://accounts.google.com/o/oauth2/auth?scope=https://www.googleapis.com/auth/androidpublisher&response_type=code&access_type=offline&redirect_uri=https://developers.google.com&client_id=111222333-aaabbbccc.apps.googleusercontent.com
输入之后,就会跳转到这个界面,就按照提示选择你的登录邮箱登录,之后一直continue,最后,url里就替换成了code和scope

输入连接后.png

 

替换成code和scope的效果

 

https://developers.google.com/?code=123456789&scope=https12345678Fandroidpublisher
image.png

我们需要的有效数据是code的值,拿到code的值之后进行urldecode解码

13、通过code获取refresh_token

使用postman或者curl,或者你熟悉的http工具,创建http请求,postman的请求大致如下:

地址:https://accounts.google.com/o/oauth2/token
请求方式:post
参数:
grant_type=authorization_code
code=获取到的code(需要看看code中是否有%号,如果有需要urldecode)
client_id=创建api项目是的clientId(客户端ID)
client_secret=创建api项目时的clientSecret(客户端密钥)
redirect_uri=创建api项目时的重定向地址

image.png

获取成功,refresh_token就是我们需要的值, 用来定时更新access_token.refresh token基本是不变的,除非你重新再走一遍上面的流程,刷新了refresh token。

image.png

 

如果此处返回,这个错误

image.png

应该是你上一步获取的code过期了,重新执行一下第12步,获取新的code替换下就好。

包过审之后需要注意

之前申请得到的refresh_token的默认有效期是7天,需要在包过审之后,登录Google Cloud Platform后台,在OAuth consent screen中对应用的状态进行修改,改成In production状态(发布中),然后再重新获取一遍refresh_token

image.png

【碰到的问题】

一、请求https://accounts.google.com/o/oauth2/token的时候,返回的值没有refresh_token(Not receiving Google OAuth refresh token)

 

image.png

可能的原因及解决方式:https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token

THE END