Ala*_*nch 5 delphi indy bad-request access-token google-oauth
我正在尝试使用 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);
FreeAndNil(Resp);
end;
end;
Run Code Online (Sandbox Code Playgroud)
编辑:
我将 Accept 标头更改为我在其他地方找到的设置。
我添加charset=utf-8到内容类型。
我替换了clientID和clientSecret。
这是 IdHTTP 发送的内容:
POST /o/oauth2/token HTTP/1.1<br>
Content-Type: application/x-www-form-urlencoded; charset=utf-8<br>
Content-Length: 240<br>
Host: accounts.google.com<br>
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8<br>
Accept-Encoding: identity<br>
User-Agent: Mozilla/3.0 (compatible; Indy Library)<br>
Run Code Online (Sandbox Code Playgroud)
client_id=clientID&client_secret=clientSecret&code=4%2FtmTyuvpcSMIkoyJcFmicfXQEDpYiw_QilkO2dXv_nUc&redirect_uri=http%3A%2F%2Flocalhost&grant_type=authorization_code