我正在使用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://tools.ietf.org/html/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

关于delphi - Google通讯录 token 已过期,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42000720/

10-14 19:38
查看更多