Google OAuth 2 authorization - Error: redirect_uri_mismatch

前端 未结 30 2347
遥遥无期
遥遥无期 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条回答
  •  Happy的楠姐
    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

提交回复
热议问题