【问题标题】:Indy10 Google Securetoken always 403Indy10 Google Securetoken 总是 403
【发布时间】:2020-05-29 11:48:12
【问题描述】:

我正在努力使用 Delphi 10.2.2 和 Indy 10 从 Google Securetoken 获得响应。

我之前从https://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword?key=XXX 获得了我的 RefreshToken。这很好用。

现在我尝试使用https://securetoken.googleapis.com/v1/token 刷新令牌,但不起作用。我总是收到403 Forbidden 错误。

procedure TForm1.Button2Click(Sender: TObject);
var
    l_Response: string;
    l_PostData: TIdMultiPartFormDataStream;
    l_IRESAccessToken: TIRESAccessToken;
begin
    IdHTTP2.Request.ContentType := 'application/json';
    IdHTTP2.Request.CharSet := 'utf-8';

    l_PostData := TIdMultiPartFormDataStream.Create;
    try
        l_PostData.AddFormField('grant_type', 'refresh_token');
        l_PostData.AddFormField('refresh_token', m_IRESAuth.RefreshToken);
        l_PostData.AddFormField('key', 'XXX');

        try
            l_Response := IdHTTP2.Post('https://securetoken.googleapis.com/v1/token', l_PostData);
            l_IRESAccessToken := TJson.JsonToObject<TIRESAccessToken>(l_Response);
        except
            on E: Exception do
                ShowMessage('Error on request: '#13#10 + e.Message);
        end;
    finally
        l_PostData.Free;
    end;
end;

我尝试了指定 TLS 1.2 的 IOHandler,并尝试使用 JSON 对象发送帖子。另外,我在TIdHTTP.HTTPOptions 中设置了hoKeepOrigProtocol 标志。到目前为止没有任何效果,我总是得到403 Forbidden

我用另一个程序试过了,没有问题。

我错过了什么吗?

【问题讨论】:

  • 你为什么使用TIdMultiPartFormDataStream
  • 我尝试使用 TStringList、JSON 和 TIdMultiPartFormDataStream 作为参数。总是遇到同样的 403 错误

标签: delphi indy indy10


【解决方案1】:

根据 Google 的文档:

Token Service REST API Reference:

HTTP 请求

POST https://securetoken.googleapis.com/v1/token

请求正文

请求正文包含具有以下结构的数据:

URL 编码表示

grant_type=string&amp;code=string&amp;refresh_token=string

TIdHTTP.Post()TStrings 重载以该格式发送数据。那是您需要使用的重载,而不是 TIdMultipartFormDataStream 重载。

此外,正如您在上面看到的,此 URL 不接受您的 key 作为输入。它只接受grant_typecode(当grant_type不是'authorization_code'时被忽略)和refresh_token

试试这个:

procedure TForm1.Button2Click(Sender: TObject);
var
  l_Response: string;
  l_PostData: TStringList;
  l_IRESAccessToken: TIRESAccessToken;
begin
  IdHTTP2.Request.ContentType := 'application/x-www-webform-urlencoded';

  l_PostData := TStringList.Create;
  try
    l_PostData.Add('grant_type=refresh_token');
    l_PostData.Add('refresh_token=' + m_IRESAuth.RefreshToken);

    try
      l_Response := IdHTTP2.Post('https://securetoken.googleapis.com/v1/token', l_PostData);
      l_IRESAccessToken := TJson.JsonToObject<TIRESAccessToken>(l_Response);
    except
      on E: Exception do
        ShowMessage('Error on request: '#13#10 + e.Message);
    end;
  finally
    l_PostData.Free;
  end;
end;

话虽如此,在阅读了上述文档之后,我不相信您可以将idTokenhttps://www.googleapis.com/identitytoolkit/v3/relyingparty/verifyPassword 发送到https://securetoken.googleapis.com/v1/token 作为refresh_token。我认为您需要将其作为authorization_code 发送,然后它会为您提供refresh_token。因此,如果您还没有这样做,请尝试添加额外的步骤。

【讨论】:

    【解决方案2】:

    抱歉回复晚了,感谢您的回答。

    在与 Main-API-Developer 协商后,他告诉我他们在后台使用 Firebase。所以 Google 的 API 请求是不同的。

    https://cloud.google.com/identity-platform/docs/use-rest-api#section-sign-in-email-password https://cloud.google.com/identity-platform/docs/use-rest-api#section-refresh-token

    由于缺少这些信息,请求可以正常工作

    
    
    procedure TForm1.Button2Click(Sender: TObject);
    var
        l_Response: string;
        l_IRESAccessToken: TIRESAccessToken;
    
        l_Json: string;
        l_JsonToSend: TStringStream;
    begin
        l_Json := '{"grant_type": "refresh_token","refresh_token": "'+ m_IRESAuth.RefreshToken+ '"}';
        l_JsonToSend := TStringStream.Create(l_Json, TEncoding.UTF8);
        try
            try
                l_Response := IdHTTP2.Post('https://securetoken.googleapis.com/v1/token?key=XXX, l_JsonToSend);
                l_IRESAccessToken := TJson.JsonToObject<TIRESAccessToken>(l_Response);
            except
                on E: Exception do
                    ShowMessage('Error on request: '#13#10 + e.Message);
            end;
        finally
            l_JsonToSend.Free;
        end;
    end;
    

    【讨论】:

      猜你喜欢
      • 2014-12-12
      • 1970-01-01
      • 2017-09-28
      • 2019-05-15
      • 2015-12-23
      • 2018-08-15
      • 2021-03-23
      • 2015-10-23
      • 2018-08-28
      相关资源
      最近更新 更多