Not a valid origin for the client from Google API Oauth

JavascriptOauth 2.0Google Oauth

Javascript Problem Overview


I'm receiving this error from Google API Oauth:

>idpiframe_initialization_failed", details: "Not a valid origin for the client: http://127.0.0.…itelist this origin for your project's client ID

I'm trying to send a request from this local path:

>http://127.0.0.1:8887/

And I already added this URL to the Authorized JavaScript origins section: enter image description here

This is my code:

<!-- The top of file index.html -->
<html itemscope itemtype="http://schema.org/Article">
<head>
  <!-- BEGIN Pre-requisites -->
  <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
  </script>
  <script src="https://apis.google.com/js/client:platform.js?onload=start" async defer>
  </script>
  <!-- END Pre-requisites -->

<!-- Continuing the <head> section -->
  <script>
    function start() {
      gapi.load('auth2', function() {
        auth2 = gapi.auth2.init({
          client_id: 'MY CLIENT ID.apps.googleusercontent.com',
          // Scopes to request in addition to 'profile' and 'email'
          //scope: 'https://www.google.com/m8/feeds/'
        });
      });
    }
  </script>




</head>
<body>


<button id="signinButton">Sign in with Google</button>
<script>
  $('#signinButton').click(function() {
    // signInCallback defined in step 6.
    auth2.grantOfflineAccess().then(signInCallback);
  });
</script>



<!-- Last part of BODY element in file index.html -->
<script>
function signInCallback(authResult) {
  if (authResult['code']) {

    // Hide the sign-in button now that the user is authorized, for example:
    $('#signinButton').attr('style', 'display: none');

    // Send the code to the server
    $.ajax({
      type: 'POST',
      url: 'http://example.com/storeauthcode',
      // Always include an `X-Requested-With` header in every AJAX request,
      // to protect against CSRF attacks.
      headers: {
        'X-Requested-With': 'XMLHttpRequest'
      },
      contentType: 'application/octet-stream; charset=utf-8',
      success: function(result) {
        // Handle or verify the server response.
      },
      processData: false,
      data: authResult['code']
    });
  } else {
    // There was an error.
  }
}
</script>
  <!-- ... -->
</body>
</html>

How can I fix this?

Javascript Solutions


Solution 1 - Javascript

Reseting Chrome cached solved it for me. Long press on Reload button, then Empty Cache and Hard Reload.

Note: Make sure your Chrome Dev tools panel is open otherwise long press wont work.

enter image description here

Solution 2 - Javascript

I had a very similar issue to yours. I tried to add multiple whitelisted ports from localhost and nothing was working. Ended up deleting the credentials and setting them up again. Must have been a bug on googles end for my setup.

Solution 3 - Javascript

If it's all the same to you, try adding http://localhost:8887 to your authorized JavaScript origins instead. Had that error myself at some point and this fixed it. Know that you will have to use this URL for your request as well event though it translates to http://127.0.0.1:8887/.

Solution 4 - Javascript

I read on several places on the web people use to redo the creation of the credentials to get it to work.
So I did, I created a new credential for the same project and used my new user-id and it worked perfectly... Looks like the edition of the whitelist is a bit flacky...

Nb: I also used localhost instead of 127.0.0.1, IPs are not valid.

Solution 5 - Javascript

I fiddled around for about 10 minutes and then it finally worked when I tried http://localhost/ in the browser (instead of 127.0.0.1)

Added the url at every place you can do white-lists at: https://console.developers.google.com/apis/credentials/

Solution 6 - Javascript

I had this same issue; but this is what worked for me:

  1. Open console.developers
  2. Open the project name
  3. Click on the credentials
  4. Under the "name", click on the "web client 1"
  5. Under the "URLs", add "**http://localhost:3000**"

Solution 7 - Javascript

just my 2 cents.. was able to get it working after deleting and recreating the credentials. Just as suggested above.

Solution 8 - Javascript

"Not a valid origin for the client" seems to be over-used by Google's API, i.e. it's misleadingly used for authentication errors too.

For people seeing the error, check that the credentials are correct.

(This might explain why it works for some people after re-creating credentials - in some cases, the original credentials might not have been correct).

Solution 9 - Javascript

I solved via adding both http://localhost and http://localhost:8083.

Solution 10 - Javascript

In case anyone missed this, next to the save button it does say:

> Note: It may take 5 minutes to a few hours for settings to take effect

Waiting fixed this issue for me.

Solution 11 - Javascript

I just went through all of these solutions before realizing I was putting in

https://localhost:3000

and my dev server was serving up

http://localhost:3000

Stupid, I know, but someone else will probably make the same mistake and perhaps this comment will help them :)

Solution 12 - Javascript

What worked for us was adding a non-localhost domain to the authorized origins. My colleague had his localhost-domains working after adding a non-existing local domain, e.g. http://test-my-app.local.

Solution 13 - Javascript

It might be in case, while you are using same email id for creating client id and for sign-in through webpage

Solution 14 - Javascript

Okay so this is super embarrasing, but for me I was following the docs for the Google Sign-in Web package for a Flutter app, and where it says:

> On your web/index.html file, add the following meta tag, somewhere in the head of the document: <meta name="google-signin-client_id" content="YOUR_GOOGLE_SIGN_IN_OAUTH_CLIENT_ID.apps.googleusercontent.com">

I had copied what was listed as my Client ID and pasted it at the beginning and had therefor duplicated the apps.googleusercontent.com portion of the content label in the meta tag. So it might help to make sure you haven't duplicated that!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionFilipe FerminianoView Question on Stackoverflow
Solution 1 - JavascriptKirill KovalevskiyView Answer on Stackoverflow
Solution 2 - JavascriptAndrew McOlashView Answer on Stackoverflow
Solution 3 - Javascriptuser7191932View Answer on Stackoverflow
Solution 4 - JavascriptVincentTellierView Answer on Stackoverflow
Solution 5 - Javascriptuser5886944View Answer on Stackoverflow
Solution 6 - JavascriptPhilipediaView Answer on Stackoverflow
Solution 7 - Javascriptuser2819304View Answer on Stackoverflow
Solution 8 - JavascriptmahemoffView Answer on Stackoverflow
Solution 9 - Javascriptuser1867537View Answer on Stackoverflow
Solution 10 - JavascriptAlistairView Answer on Stackoverflow
Solution 11 - JavascriptTomHinkleView Answer on Stackoverflow
Solution 12 - JavascriptRexView Answer on Stackoverflow
Solution 13 - JavascriptSahil MujawarView Answer on Stackoverflow
Solution 14 - JavascriptTristan BennettView Answer on Stackoverflow