Google OAuth 2 authorization - Error: redirect_uri_mismatch

前端 未结 30 1926
遥遥无期
遥遥无期 2020-11-22 02:58

On the website https://code.google.com/apis/console I have registered my application, set up generated Client ID: and Client Secret to my a

相关标签:
30条回答
  • 2020-11-22 03:11

    Just make sure that you are entering URL and not just a domain. So instead of: domain.com it should be domain.com/somePathWhereYouHadleYourRedirect

    0 讨论(0)
  • 2020-11-22 03:11

    What you need to do is go back to your Developer Console and go to APIs & Auth > Consent Screen and fill that out. Specifically, the product name.

    0 讨论(0)
  • 2020-11-22 03:13

    In any flow where you retrieved an authorization code on the client side, such as the GoogleAuth.grantOfflineAccess() API, and now you want to pass the code to your server, redeem it, and store the access and refresh tokens, then you have to use the literal string postmessage instead of the redirect_uri.

    For example, building on the snippet in the Ruby doc:

    client_secrets = Google::APIClient::ClientSecrets.load('client_secrets.json')
    auth_client = client_secrets.to_authorization
    auth_client.update!(
      :scope => 'profile https://www.googleapis.com/auth/drive.metadata.readonly',
      :redirect_uri => 'postmessage' # <---- HERE
    )
    
    # Inject user's auth_code here:
    auth_client.code = "4/lRCuOXzLMIzqrG4XU9RmWw8k1n3jvUgsI790Hk1s3FI"
    tokens = auth_client.fetch_access_token!
    # { "access_token"=>..., "expires_in"=>3587, "id_token"=>..., "refresh_token"=>..., "token_type"=>"Bearer"}
    

    The only Google documentation to even mention postmessage is this old Google+ sign-in doc. Here's a screenshot and archive link since G+ is closing and this link will likely go away:

    It is absolutely unforgivable that the doc page for Offline Access doesn't mention this. #FacePalm

    0 讨论(0)
  • 2020-11-22 03:13

    Rails users (from the omniauth-google-oauth2 docs):

    Fixing Protocol Mismatch for redirect_uri in Rails

    Just set the full_host in OmniAuth based on the Rails.env.

    # config/initializers/omniauth.rb

    OmniAuth.config.full_host = Rails.env.production? ? 'https://domain.com' : 'http://localhost:3000'

    REMEMBER: Do not include the trailing "/"

    0 讨论(0)
  • 2020-11-22 03:13

    Anyone struggling to find where to set redirect urls in the new console: APIs & Auth -> Credentials -> OAuth 2.0 client IDs -> Click the link to find all your redirect urls

    0 讨论(0)
  • 2020-11-22 03:14

    2015July15 - the signin that was working last week with this script on login

    <script src="https://apis.google.com/js/platform.js" async defer></script>
    

    stopped working and started causing Error 400 with Error: redirect_uri_mismatch

    and in the DETAILS section: redirect_uri=storagerelay://...

    i solved it by changing to:

    <script src="https://apis.google.com/js/client:platform.js?onload=startApp"></script>
    
    0 讨论(0)
提交回复
热议问题