我正在尝试使用oauth连接到Google Calendar Api。我是否形成了错误的请求或我的刷新令牌已过期?
URLString := 'client_id=' + FClientID;
URLString := URLString + '&client_secret=' + FClientSecret;
URLString := URLString + '&refresh_token=' + FRefreshToken;
URLString := URLString + '&grant_type=refresh_token';
Req := TStringStream.Create(URLString);
Resp := TStringStream.Create('');
FHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
FHTTP.DoRequest('POST', 'https://accounts.google.com/o/oauth2/token', Req, Resp, []);
{ Response was:
======================
Recv 31.12.2017. 14:12:59: HTTP/1.1 400 Bad Request
1f
{
"error" : "invalid_grant"
}
}
后来我成功地获得了用户的同意。事情是 - 我不想每次都要求用户同意。也许用户超过50个刷新令牌限制?我尝试在Google控制台中创建新的Api,打赌这没有帮助。
成功代码,如果下面没有刷新令牌:
if Assigned(FOnAuthorizationContinueQuery) then
FOnAuthorizationContinueQuery(Self, OkResult, InputResult)
else
OkResult := InputQuery(Application.Title, 'Enter your code here:', InputResult);
if OkResult then
FCode := InputResult { that code I copy/pasted from browser }
else
Exit;
URLString := 'client_id=' + FClientID;
URLString := URLString + '&client_secret=' + FClientSecret;
URLString := URLString + '&code=' + FCode;
URLString := URLString + '&redirect_uri=urn:ietf:wg:oauth:2.0:oob';
URLString := URLString + '&grant_type=authorization_code';
Req := TStringStream.Create(URLString);
Resp := TStringStream.Create('');
try
try
FHTTP.Request.ContentType := 'application/x-www-form-urlencoded';
FHTTP.DoRequest('POST', 'https://accounts.google.com/o/oauth2/token', Req, Resp, []);
{ Response was:
======================
Recv 31.12.2017. 14:13:09: HTTP/1.1 200 OK
X-Content-Type-Options: nosniff
Content-Disposition: attachment; filename="json.txt"; filename*=UTF-8''json.txt
111
{
"access_token" : "token value was here",
"expires_in" : 3600,
"refresh_token" : "1/XI11nmVZdOuB1IfBVMEovNffKTKTGgJ5wsId1EwQE9U",
"token_type" : "Bearer"
}
}