错误:redirect_uri_mismatch Google API oauth2获取访问令牌 [英] Error: redirect_uri_mismatch google api oauth2 to get access token

查看:145
本文介绍了错误:redirect_uri_mismatch Google API oauth2获取访问令牌的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Getting following error in my code.

400错误的请求 { 错误":"redirect_uri_mismatch" }

400 Bad Request { "error" : "redirect_uri_mismatch" }

我不明白这是怎么回事.我似乎在正确地使用Google api的规范中所定义的.在开发人员控制台中创建安装的应用程序后,我从浏览器获取了授权代码并将其插入.从控制台中提取了redirect_uri.谁能指出我redirect_uri出了什么问题.我一直无法弄清楚该参数出了什么问题.

I don't understand what is wrong. I seemed to be using google api's correctly as defined in their specifications. After creating a installed app in the developer console I got the authorization code from the browser and plugged it in. The redirect_uri was picked from the console. Can anyone point to me what is wrong with the redirect_uri. I haven't been able to figure out what is wrong with that parameter.

    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);

    }
}

推荐答案

您的指针正确.

我为错误的问题on了两天.

I was scratching my head for two days on what was wrong.

显然,redirect_uri需要在auth和令牌请求之间匹配.

Apparently the redirect_uri need to match between auth and token requests.

如果不是,则将引发redirect_uri_mismatch.

The redirect_uri_mismatch will be thrown if it is not.

google api的文档说要使用 developer-console 中的 redirect_uri .
但是,他们自动生成并粘贴到浏览器中的URL不会执行此操作,而是使用与 localhost + port 不同的url.
这是问题的根本根源.

The documentation for google api says to use the redirect_uri from the developer-console.
However, the url that they auto-generate, to paste in the browser, doesn't do that, and uses a localhost + port url that is different.
This was fundamental source of problem.

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

If you take the auth url, that they asked you to cut and paste in browser, and plug in the correct redirect_uri "urn:ietf:wg:oauth:2.0:oob" from the console and get the code, then use the same redirect_uri to get the token, then there is no redirect_uri_mismatch.
Thanks for your answer.

这篇关于错误:redirect_uri_mismatch Google API oauth2获取访问令牌的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆