Google 联系人令牌过期

Google Contacts token expires

我与 Google 联系人合作,一切顺利。但是一个小时后,我需要手动刷新授予的访问权限。根据此 SO question 我应该可以通过此调用更新我的 refresh_token:

refresh_token = requests.post(
  'https://accounts.google.com/o/oauth2/token',
   data={
       'client_id': APP_ID,
       'client_secret': APP_SECRET,
       'refresh_token': refresh_token,
       'grant_type': 'refresh_token',
   }
  )

我已将其翻译成此 Delphi 代码

var
  Http: TidHttp;
  URLString: string;
  Req, Resp: TStringStream;
begin
  Http := TidHttp.Create(nil);
  try
    URLString := 'client_id=' + FGoggleContacts.ClientID;
    URLString := URLString + '&client_secret=' + FGoggleContacts.ClientSecret;
    URLString := URLString + '&refresh_token=' + FGoggleContacts.RefreshToken;
    URLString := URLString + '&grant_type=refresh_token';

    Req := TStringStream.Create(URLString);
    Resp := TStringStream.Create('');
    HTTP.DoRequest(Id_HTTPMethodPost, 'https://accounts.google.com/o/oauth2/token', Req, Resp, []);

  finally
    Req.Free;
    Resp.Free;
    Http.Free;
  end;    
end;

但是调用它时出现 http/1.1 400 bad request 错误

简而言之,如何保持与 Google 的连接?

更新

在@mjn42 的帮助下,我发现我的请求中缺少 Content-Type。所以我写了一个新方法来刷新我的令牌:

procedure TGContacts.RefreshTokens;
var
  Http: TidHttp;
  URLString: string;
  Req, Resp: TStringStream;
  JSon: ISuperObject;
begin
  Http := TidHttp.Create(nil);
  Req := TStringStream.Create('');
  Resp := TStringStream.Create('');
  try
    URLString := 'client_id=' + FClientID;
    URLString := URLString + '&client_secret=' + FClientSecret;
    URLString := URLString + '&refresh_token=' + FRefreshToken;
    URLString := URLString + '&grant_type=refresh_token';
    Req.WriteString(URLString);

    HTTP.Request.ContentType := 'application/x-www-form-urlencoded';
    HTTP.DoRequest(Id_HTTPMethodPost, 'https://accounts.google.com/o/oauth2/token', Req, Resp, []);
    if HTTP.ResponseCode = 200 then
    begin
      JSon := SO(UTF8Decode(Resp.DataString));
      FAccessToken := JSon['access_token'].AsString;
      if JSon['refresh_token'] <> nil then
        FRefreshToken := JSon['refresh_token'].AsString;
    end;

  finally
    Req.Free;
    Resp.Free;
    Http.Free;
  end;
end;

我只是在访问 API 之前调用它,这样令牌就不会过期。

根据 https://www.rfc-editor.org/rfc/rfc6749#section-6 的规范,请求应使用 Content-Type: application/x-www-form-urlencoded:

POST /token HTTP/1.1
Host: server.example.com
Authorization: Basic czZCaGRSa3F0MzpnWDFmQmF0M2JW
Content-Type: application/x-www-form-urlencoded

grant_type=refresh_token&refresh_token=tGzv3JOkF0XG5Qx2TlKWIA