Google oauth2 api 客户端无法正常工作 [英] Google oauth2 api client is not working properly

查看:27
本文介绍了Google oauth2 api 客户端无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 grails 2.3.4 的控制器操作中有一些代码使用 google java 客户端库来访问 OAuth2 api.但是当我创建一个 GoogleAuthorizationCodeFlow 实例时,我收到了 redirect_uri_mismatch 错误.google 给我的 url 是这个 http://localhost:60720/Callback,而我在 google api 控制台中将回调 url 定义为这个 http://localhost:8080/<myAppName>//.当我将重定向的 url 手动复制粘贴到地址栏中以替换 google 给我的那个时,我的应用程序运行良好.

I have some code in my grails 2.3.4's controller's action that uses google java client libraries to access the OAuth2 api. But when I create an instance of GoogleAuthorizationCodeFlow I get redirect_uri_mismatch error. The url google gives me is this http://localhost:60720/Callback, while I have defined the callback url in the google api console as this http://localhost:8080/<myAppName>/<controllerName>/<actionName>. When I copy paste my redirected url manually in the address bar replacing the one google gave me, my application works well.

我已在 api 控制台中将该应用程序注册为 Web 应用程序未安装的应用程序.我能做什么?请帮忙.如果我不能解决这个问题,那么我将恢复到 REST Api.

I've registered the application as web application not installed application in api console. What can I do? Please help. If I couldn't solve this problem then I'll revert to the REST Api.

ResourceLocator grailsResourceLocator



JsonFactory jsonFactory = JacksonFactory.defaultInstance

File clientSecretsFile = grailsResourceLocator.findResourceForURI("/configs/clientSecrets.json").file

GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(jsonFactory, new InputStreamReader(new FileInputStream(clientSecretsFile)))

HttpTransport httpTransport = GoogleNetHttpTransport.newTrustedTransport()

FileDataStoreFactory dataStoreFactory = new FileDataStoreFactory(new File(System.getProperty("user.home"), ".store/oauth2_sample"))

List<String> SCOPES = ["https://www.googleapis.com/auth/userinfo.profile", "https://www.googleapis.com/auth/userinfo.email"]

GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder(httpTransport, jsonFactory, clientSecrets, SCOPES).setDataStoreFactory(dataStoreFactory).build()

Credential credential = new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()).authorize("user")

谢谢.

推荐答案

好吧,我找到了解决方案.

Okey I've found the solution.

当你创建 new LocalServerReceiver() 时使用 new LocalServerReceiver.Builder().setPort(9089).build()

在这种情况下,我选择了一些空端口 9089.实际上 LocalServerReceiver 是一个 http 服务器,它侦听 google 并且 google 将 code 参数发送到该服务器.现在您要做的就是在重定向的 URI 中创建一个新条目,在本例中如下所示:http://localhost:9089/Callback.请注意,端口号与我在使用 LocalServerReceiver.Builder() 类构建服务器时在代码中使用的相同.

I've chosen some empty port in this case 9089. Actually LocalServerReceiver is a http server that listens to google and google sends code param to this server. Now all you have to do is create a new entry in your Redirected URIs which is in this case as follows: http://localhost:9089/Callback. Note the port number is same as I've used in the code while building the server using LocalServerReceiver.Builder() class.

瞧!!!你从谷歌获得了 access_token 和 refresh_token.

And voila!!! you got the access_token and refresh_token from the google.

愉快的编码...愉快的 Java 客户...

Happy coding... Happy Java clients...

这篇关于Google oauth2 api 客户端无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆