Using refresh_token for Google OAuth 2.0 returns http 400 bad request

后端 未结 2 1694
生来不讨喜
生来不讨喜 2021-01-06 01:29

I am using a server-side flow validation for an app that connects to Google Drive.

I am able to retrieve the access code and exchange for an access_token and user in

相关标签:
2条回答
  • 2021-01-06 02:01

    Okay, I figured it out. This is what the Google OAauth website says the post request needs to look like:

    POST /o/oauth2/token HTTP/1.1
    Host: accounts.google.com
    Content-Type: application/x-www-form-urlencoded
    
    client_id=8819981768.apps.googleusercontent.com&
    client_secret={client_secret}&
    refresh_token=1/6BMfW9j53gdGImsiyUH5kU5RsR4zwI9lUVX-tqf8JXQ&
    grant_type=refresh_token
    

    If I change my httpie to use the --form switch instead of adding a ContentType header, then I do get an access token back:

     % http --verbose --form POST https://accounts.google.com/o/oauth2/token refresh_token=1/nJZGF7hIySVtVCl8I-Y3KfXAPk84gD0X6ym7hQS8gcc client_id=XXXX client_secret=XXXX grant_type=refresh_token                                                                       POST /o/oauth2/token HTTP/1.1
    Content-Length: 175
    Content-Type: application/x-www-form-urlencoded
    Host: accounts.google.com
    b'Accept': */*
    b'Accept-Encoding': gzip, deflate, compress
    b'Content-Type': application/x-www-form-urlencoded; charset=utf-8
    b'User-Agent': HTTPie/0.6.0
    
    refresh_token=1%2FnJZGF7hIySVtVCl8I-Y3KfXAPk84gD0X6ym7hQS8gcc&client_id=XXXX&client_secret=XXXX&grant_type=refresh_token
    
    HTTP/1.1 200 OK
    Alternate-Protocol: 443:quic
    Cache-Control: no-cache, no-store, max-age=0, must-revalidate
    Content-Type: application/json
    Date: Mon, 16 Sep 2013 05:20:21 GMT
    Expires: Fri, 01 Jan 1990 00:00:00 GMT
    Pragma: no-cache
    Server: GSE
    Transfer-Encoding: chunked
    X-Content-Type-Options: nosniff
    X-Frame-Options: SAMEORIGIN
    X-XSS-Protection: 1; mode=block
    
    {
      "access_token": "XXXX", 
      "expires_in": 3600, 
      "id_token": "eyJhbGciOiJSUzI1NiIsImtpZCI6IjRlNDZiMGQ4Zjg1OWRhMDNjOGM3MmY5YTM3ZWM0NTFjM2RjNTM0NmUifQ.eyJpc3MiOiJhY2NvdW50cy5nb29nbGUuY29tIiwic3ViIjoiMTE3Mzk3NDI0ODc1MDc4OTM1MDY2IiwiYXRfaGFzaCI6IkJvT0lCZVVXcmthRzRBY2NpajZkaEEiLCJhdWQiOiI2NDIzMDEzNjM0NDQuYXBwcy5nb29nbGV1c2VyY29udGVudC5jb20iLCJlbWFpbCI6InNjb2xpbmNydTAwMUBteW1haWwubGF1c2QubmV0IiwiZW1haWxfdmVyaWZpZWQiOiJ0cnVlIiwiYXpwIjoiNjQyMzAxMzYzNDQ0LmFwcHMuZ29vZ2xldXNlcmNvbnRlbnQuY29tIiwiaGQiOiJteW1haWwubGF1c2QubmV0IiwiaWF0IjoxMzc5MzA4NTIxLCJleHAiOjEzNzkzMTI0MjF9.XtEDuIaEK5qe0SIFVr2l88zu3FpPBKl3_9z0D0wMCOxE-lnC4abrL71uxvMbVHvTVNbcFRs5RPHTrwPtidfw44MoukZLwVaW1c1TYBet2yuC3bZeoe7HPBZxzdMmpqBiYZOkvru3o_S5kaGp1csKzttd_fZ9nkzXITSMHxHAtbk", 
      "token_type": "Bearer"
    }
    

    So, I need to have Content-Type: "application/x-www-form-urlencoded; charset=utf-8", instead of Content-Type: "application/x-www-form-urlencoded" and that fixes the problem.

    0 讨论(0)
  • 2021-01-06 02:01

    In my case, I'm using iOS mobile app-based authentication, and I was forgetting that to get a refresh token that can be used on the server side you have to obtain an authorization code on the mobile app, send that to the server, and the server gets its refresh token from a REST call to Google using that authorization code (see https://developers.google.com/identity/sign-in/ios/offline-access?hl=en).

    0 讨论(0)
提交回复
热议问题