【Google pay接入】获取refresh_token
开发相关导入包及配置这里就不多说,只针对GP后台操作做个记录
1、先打开Google Play Console后台,在API access中创建或绑定 Google Cloud Platform中的项目,这边是创建,创建之后点击“View project”可直接跳转至Google Cloud Platform后台
![](http://upload-images.jianshu.io/upload_images/4237968-c9aab1fd7e0177a7.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
2、在Google Cloud Platform后台打开APIs and Services
![](http://upload-images.jianshu.io/upload_images/4237968-6f3696e74e9d56d8.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
3、开启OAuth consent screen
![](http://upload-images.jianshu.io/upload_images/4237968-cde17d4be11fff92.png?imageMogr2/auto-orient/strip|imageView2/2/w/1129/format/webp)
4、填写相关信息
![](http://upload-images.jianshu.io/upload_images/4237968-6e923da860066901.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
![](http://upload-images.jianshu.io/upload_images/4237968-e420929704b63cdc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1078/format/webp)
5、到第二步“Scopes”先直接跳过
![](http://upload-images.jianshu.io/upload_images/4237968-ac06e4366120f56a.png?imageMogr2/auto-orient/strip|imageView2/2/w/969/format/webp)
6、第三步“Test users”可添加测试人员账号
![](http://upload-images.jianshu.io/upload_images/4237968-5db57c17c6b7e6ce.png?imageMogr2/auto-orient/strip|imageView2/2/w/976/format/webp)
7、最后一步直接返回,至此完成了OAuth consent screen的创建
![](http://upload-images.jianshu.io/upload_images/4237968-94ed36c4c2f5d45f.png?imageMogr2/auto-orient/strip|imageView2/2/w/1151/format/webp)
8、创建web client
![](http://upload-images.jianshu.io/upload_images/4237968-ab3888e048a40da4.png?imageMogr2/auto-orient/strip|imageView2/2/w/1163/format/webp)
![](http://upload-images.jianshu.io/upload_images/4237968-bdf1cf0f5e8a9cda.png?imageMogr2/auto-orient/strip|imageView2/2/w/1170/format/webp)
![](http://upload-images.jianshu.io/upload_images/4237968-c6a258ac963df9b1.png?imageMogr2/auto-orient/strip|imageView2/2/w/1087/format/webp)
9、创建成功后获得Client ID 和Client Secret
![](http://upload-images.jianshu.io/upload_images/4237968-91d0c0626e7a65c6.png?imageMogr2/auto-orient/strip|imageView2/2/w/725/format/webp)
10、在首页也能看到创建好的client
![](http://upload-images.jianshu.io/upload_images/4237968-04a69540f75a926a.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
11、返回Google Play Console控制后台可以看到此项目
![](http://upload-images.jianshu.io/upload_images/4237968-1af36dba835517bc.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
至此,相关的设置已经完成,接下来拿到项目的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
![](http://upload-images.jianshu.io/upload_images/4237968-c6a6028a4344a7bf.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
替换成code和scope的效果
https://developers.google.com/?code=123456789&scope=https12345678Fandroidpublisher
![](http://upload-images.jianshu.io/upload_images/4237968-0b3118be9fda4261.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
我们需要的有效数据是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项目时的重定向地址
![](http://upload-images.jianshu.io/upload_images/4237968-609626d47eaa06b0.png?imageMogr2/auto-orient/strip|imageView2/2/w/1107/format/webp)
获取成功,refresh_token就是我们需要的值, 用来定时更新access_token.refresh token基本是不变的,除非你重新再走一遍上面的流程,刷新了refresh token。
![](http://upload-images.jianshu.io/upload_images/4237968-c086bea0ffa66ca2.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
如果此处返回,这个错误
![](http://upload-images.jianshu.io/upload_images/4237968-a7c767738104f166.png?imageMogr2/auto-orient/strip|imageView2/2/w/652/format/webp)
应该是你上一步获取的code过期了,重新执行一下第12步,获取新的code替换下就好。
包过审之后需要注意
之前申请得到的refresh_token的默认有效期是7天,需要在包过审之后,登录Google Cloud Platform后台,在OAuth consent screen中对应用的状态进行修改,改成In production状态(发布中),然后再重新获取一遍refresh_token
![](http://upload-images.jianshu.io/upload_images/4237968-ceecc904bcaf1b88.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)
【碰到的问题】
一、请求https://accounts.google.com/o/oauth2/token的时候,返回的值没有refresh_token(Not receiving Google OAuth refresh token)
![](http://upload-images.jianshu.io/upload_images/4237968-ab1d9dc53808e174.png?imageMogr2/auto-orient/strip|imageView2/2/w/868/format/webp)
可能的原因及解决方式:https://stackoverflow.com/questions/10827920/not-receiving-google-oauth-refresh-token
![](http://upload-images.jianshu.io/upload_images/4237968-a4a32b0f36b3c69e.png?imageMogr2/auto-orient/strip|imageView2/2/w/1200/format/webp)