Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error: redirect_uri_mismatch google api oauth2 to get access token

Tags:

google-oauth

Getting following error in my code.

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

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

    }
}
like image 993
user3908106 Avatar asked Aug 04 '14 23:08

user3908106


People also ask

How do I get Google API postman access token?

Let's start. Open Postman and paste the api we want to inspect under the bar and navigate to the Authorization tab. Postman Get bar displaying the Google Classroom api address and the Authorization tab highlighted. Client ID: Retrieved from the Google Console for your Google Classroom project or the credentials.

How do I get OAuth tokens on Google Drive?

Procedure. Go to Google Developers OAuth Playground. Click OAuth 2.0 Configuration and select Use your own OAuth credentials check box, enter the OAuth client ID and client secret you have already created in the OAuth Client ID and OAuth Client secret fields respectively.


1 Answers

Nope, I didnt supply from my end a redirect_uri to get authorization code but it was pre filled with some https localhost uri by google code that I used to get the authorization code. It said cut and paste the url in the browser to get the authorization code.

The redirect_uri that I am using for the access_token request is different value and one that I cut and paste from the console "urn:ietf:wg:oauth:2.0:oob" and I do have a plain localhost preset by google in the console setting as well for redirect_uri for the installed app/other project but I don't have https web server on localhost.

Should the redirect_uri match for authorization code and access token requests? If so, what should it be for installed app/other. Should I get authorization code using "urn:ietf:wg:oauth:2.0:oob" as the redirect_uri?

like image 51
Leo Avatar answered Oct 28 '22 16:10

Leo