Google oauth2 api客户端无法正常运行

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

我已将应用程序注册为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 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") 

请帮忙….谢谢……

哦,我找到了解决方案。

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

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

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

快乐的编码……快乐的Java客户……