Man*_* SK 5 java grails client authorization
大家好我在grails 2.3.4的控制器动作中有一些代码,它使用谷歌java客户端库来访问OAuth2 api.但是当我创建一个GoogleAuthorizationCodeFlow实例时,我得到redirect_uri_mismatch错误.网址谷歌给我的是这个http://localhost:60720/Callback,而我已经在google api控制台中定义了回调网址http://localhost:8080/<myAppName>/<controllerName>/<actionName>.当我在地址栏中手动粘贴重定向的网址替换谷歌给我的一个时,我的应用程序运行良好.
我已将应用程序注册为api控制台中未安装应用程序的Web应用程序.我能做什么?请帮忙.如果我无法解决这个问题,那么我将恢复到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")
Run Code Online (Sandbox Code Playgroud)
请帮忙....谢谢......
哦,我找到了解决方案.
当您创建new LocalServerReceiver()使用时new LocalServerReceiver.Builder().setPort(9089).build()
在这种情况下,我选择了一些空端口9089.实际上LocalServerReceiver是一个监听谷歌和谷歌的http服务器发送codeparam到这个服务器.现在,您所要做的就是在重定向的URI中创建一个新条目,在这种情况下如下:http://localhost:9089/Callback.请注意,在使用LocalServerReceiver.Builder()类构建服务器时,端口号与我在代码中使用的端口号相同.
瞧!你从谷歌获得了access_token和refresh_token.
快乐的编码......快乐的Java客户......
| 归档时间: |
|
| 查看次数: |
4451 次 |
| 最近记录: |