与OAuth 2.0中的redirect_uri混淆

时间:2013-03-26 04:46:20

标签: java oauth-2.0 google-analytics-api

我一直在使用Google Analytics(分析)为我的网站created in weebly。我正在使用Google API以编程方式实现整个过程。

我在OAuth流程中遇到的问题是我收到此错误:

Error: redirect_uri_mismatch
The redirect URI in the request:localhost:34190/Callback did not match a registered redirect URI

        Request Details
        scope=https://www.googleapis.com/auth/analytics.readonly
        response_type=code
        access_type=online
        redirect_uri=local_host:34190/Callback
        display=page
        client_id={CLIENT_ID}.apps.googleusercontent.com

我的谷歌api控制台配置是:

Redirect URI: localhost/oauth2callback
JavaScript origins: localhost

当我将redirect_uri设置为localhost:34190/Callback时,为什么http://mya.com/oauth2callbackpublic static void main(String[] args) throws Exception { Analytics analytics = initializeAnalytics(); } private static Analytics initializeAnalytics() throws Exception { Credential credential = authorize(); } private static Credential authorize() throws Exception { GoogleClientSecrets clientSecrets = GoogleClientSecrets.load( JSON_FACTORY, Testcode.class.getResourceAsStream("/client_secrets.json")); FileCredentialStore credentialStore = new FileCredentialStore( new File(System.getProperty("user.home"), ".credentials/analytics.json"), JSON_FACTORY); GoogleAuthorizationCodeFlow flow = new GoogleAuthorizationCodeFlow.Builder( HTTP_TRANSPORT, JSON_FACTORY, clientSecrets, Collections.singleton(AnalyticsScopes.ANALYTICS_READONLY)).setCredentialStore( credentialStore).build(); return new AuthorizationCodeInstalledApp(flow, new LocalServerReceiver()) .authorize(clientSecrets.getDetails().getClientId()); }

我为露报写的代码:

{{1}}

如何摆脱这个错误?

1 个答案:

答案 0 :(得分:4)

因此,您的网站告知Google使用localhost:34190/Callback作为重定向URI。但是您已经向Google服务器说过,当您的应用将localhost/oauth2callback指定为重定向URI时,他才会接受请求。

简单的解决方法是按如下方式设置Google控制台:

Redirect URI: http://localhost:34190/Callback

无论如何,您所说的是想要重定向到http://mya.com/oauth2callback

在这种情况下,您应该更改用户在您网站上点击的链接以指定此

[...]&redirect_uri=http://mya.com/oauth2callback[...]

然后在Google控制台中进行设置:

Redirect URI: http://mya.com/oauth2callback