我正在尝试使用 oauth2 从 Google 请求访问和刷新令牌,我得到的只是“HTTP/1.1 400 错误请求”错误。
我在 Delphi XE5 中使用 IdHTTP 和 IdSSLIOHandlerSocketOpenSSL 处理程序。我有我的客户端 ID、客户端密钥和 API 密钥。我有授权码。
IdHTTP 配置为:
AllowCookies = True
HandleRedirects = True
HTTPOptions.hoKeepOrigProtocol = True
HTTPOptions.hoNoProtocolErrorException = True
Run Code Online (Sandbox Code Playgroud)
其他一切都是默认的。
有什么我应该事先做的事情吗,比如身份验证?
创建 Win32 应用程序时,redirect_uri 的相关性是什么?
将不胜感激地收到所有帮助。
这是我的代码:
var Params: TStringList;
Resp: TStringStream;
URI: String;
begin
Params := TStringList.Create;
Resp := TStringStream.Create;
try
URI := 'https://accounts.google.com/o/oauth2/token';
Params.Add('client_id=' + clientID);
Params.Add('client_secret=' + clientSecret);
Params.Add('code=' + authCode);
Params.Add('redirect_uri=http://localhost');
Params.Add('grant_type=authorization_code');
IdHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
IdHTTP.Post(URI, Params, Resp);
Memo1.Lines.LoadFromStream(Resp);
Memo1.Lines.Insert(0, IdHTTP.ResponseText);
finally
FreeAndNil(Params); …Run Code Online (Sandbox Code Playgroud)