【问题标题】:Error: redirect_uri_mismatch google api oauth2 to get access token错误:redirect_uri_mismatch google api oauth2 以获取访问令牌
【发布时间】:2014-08-04 23:55:13
【问题描述】:

在我的代码中出现以下错误。

400 错误请求 { “错误”:“redirect_uri_mismatch” }

我不明白出了什么问题。我似乎正确地使用了他们规范中定义的google api。在开发人员控制台中创建安装的应用程序后,我从浏览器中获取了授权代码并将其插入。redirect_uri 是从控制台中选择的。谁能指出redirect_uri有什么问题。我一直无法弄清楚那个参数有什么问题。

    import com.google.api.client.auth.oauth2.Credential;
    import com.google.api.client.extensions.java6.auth.oauth2.AuthorizationCodeInstalledApp;
    import com.google.api.client.extensions.jetty.auth.oauth2.LocalServerReceiver;
    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeFlow;

    import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest;
    import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse;

    import com.google.api.client.googleapis.auth.oauth2.GoogleClientSecrets;
    import com.google.api.client.googleapis.javanet.GoogleNetHttpTransport;
    import com.google.api.client.http.HttpTransport;
    import com.google.api.client.http.javanet.NetHttpTransport;
    import com.google.api.client.json.JsonFactory;
    import com.google.api.client.json.jackson2.JacksonFactory;
    import com.google.api.client.util.store.DataStoreFactory;
    import com.google.api.client.util.store.FileDataStoreFactory;
    import com.google.api.services.adexchangeseller.AdExchangeSeller;
    import com.google.api.services.adexchangeseller.AdExchangeSellerScopes;
    import com.google.api.services.adexchangeseller.model.AdClients;
    import com.google.api.services.adexchangeseller.model.AdUnits;
    import com.google.api.services.adexchangeseller.model.CustomChannels;
    import com.google.api.services.adexchangeseller.model.SavedReports;

    import com.google.api.services.adexchangeseller.AdExchangeSeller;
    import com.google.api.services.adexchangeseller.AdExchangeSeller.Reports.Generate;
    import com.google.api.services.adexchangeseller.model.Report;

    import java.io.FileInputStream;

    import java.text.DateFormat;
    import java.text.SimpleDateFormat;
    import java.util.Arrays;
    import java.util.Calendar;
    import java.util.Date;
    import java.util.List;

    import java.io.IOException;
    import java.io.InputStreamReader;
    import java.util.Collections;

    public class Reporting {

    public class AdXReporting {

      private static String AD_CLIENT_ID = "....";

      private static final String APPLICATION_NAME = "AdX Installed app product";

      private static final String authorizationCode = "..............";

      private static final String CALLBACK_URL = "urn:ietf:wg:oauth:2.0:oob";

      private static final JsonFactory JSON_FACTORY = JacksonFactory.getDefaultInstance();

      private static final java.io.File DATA_STORE_DIR = new java.io.File("adexchangeseller_sample");
      private static void authorize() {

        GoogleClientSecrets clientSecrets = GoogleClientSecrets.load(JSON_FACTORY,
            new InputStreamReader(new FileInputStream("client_secrets.json")));

        GoogleAuthorizationCodeFlow flow =
            new GoogleAuthorizationCodeFlow.Builder(
                httpTransport,
                JSON_FACTORY,
                clientSecrets,
                Collections.singleton(AdExchangeSellerScopes.ADEXCHANGE_SELLER_READONLY)
            ).setDataStoreFactory(dataStoreFactory).build();


        GoogleAuthorizationCodeTokenRequest tokenRequest =
            flow.newTokenRequest(authorizationCode);

        tokenRequest.setRedirectUri(CALLBACK_URL);
        GoogleTokenResponse tokenResponse = tokenRequest.execute();

        // Store the credential for the user.
        flow.createAndStoreCredential(tokenResponse, AD_CLIENT_ID);

    }
}

【问题讨论】:

  • 这个redirect_uri和你获取授权码时使用的一样吗?

标签: google-oauth


【解决方案1】:

您的指针是正确的。

这两天我一直在摸不着头脑。

显然,redirect_uri 需要在身份验证和令牌请求之间进行匹配。

如果不是,则会抛出 redirect_uri_mismatch。

google api 的文档说要使用 developer-console 中的 redirect_uri
但是,他们自动生成的要粘贴到浏览器中的 url 并没有这样做,而是使用不同的 localhost + port url。
这是问题的根本根源。

如果您使用他们要求您在浏览器中剪切和粘贴的身份验证 URL,然后从控制台插入正确的 redirect_uri "urn:ietf:wg:oauth:2.0:oob"并获取代码,然后使用相同的redirect_uri获取token,则没有redirect_uri_mismatch。
谢谢你的回答。

【讨论】:

  • 我在这个问题上浪费了大约 4 个小时。这就是问题所在。我在安装的应用程序中用于检索代码的redirect_uri 是“localhost:52800/authorize”,直到我完全匹配它,它才给出redirect_uri_mismatch。此外,52800 是一个临时端口,它选择打开以接收代码。
  • 5 年后,依然如此
  • 非常感谢。浪费了 2 天的时间来解决这个问题。顺便说一句,当我写这篇评论时,问题仍然存在......
【解决方案2】:

不,我没有从我的末端提供redirect_uri 来获取授权码,但它预先填充了一些我用来获取授权码的谷歌代码的https localhost uri。它说在浏览器中剪切并粘贴 url 以获取授权码。

我用于 access_token 请求的 redirect_uri 是不同的值,是我从控制台“urn:ietf:wg:oauth:2.0:oob”剪切和粘贴的值,我确实有一个普通的 localhost 由谷歌预设控制台设置以及已安装的应用程序/其他项目的 redirect_uri,但我在 localhost 上没有 https Web 服务器。

redirect_uri 是否应该与授权代码和访问令牌请求匹配?如果是这样,安装的应用程序/其他应该是什么。我应该使用“urn:ietf:wg:oauth:2.0:oob”作为redirect_uri 来获取授权码吗?

【讨论】:

    猜你喜欢
    • 2015-10-12
    • 2013-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-05
    • 2013-03-25
    • 1970-01-01
    • 2022-11-28
    相关资源
    最近更新 更多