1

我一直在为我在 weebly 创建的网站使用 Google Analytics 。我正在使用 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/Callbackhttp://mya.com/oauth2callback

我为 openauth 编写的代码:

public 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());
  }       

我怎样才能摆脱这个错误?

4

1 回答 1

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 Console 中进行设置:

Redirect URI: http://mya.com/oauth2callback
于 2013-03-26T11:51:59.203 回答