Delphi-Google Firebase-HTTP [英] Delphi - Google Firebase - HTTP

查看:147
本文介绍了Delphi-Google Firebase-HTTP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过HTTP在Firebase上通过代码或REST调试器通过HTTP发送 POST 请求,但返回错误:

I'm trying to send a POST request at Firebase via HTTP, either in code or with the REST Debugger, but it returns an error:


HTTP / 1.1 401请求缺少身份验证密钥(FCM令牌)。请参阅FCM文档的身份验证部分,网址为https = // firebase.google.com/docs

HTTP/1.1 401 The request was missing an Authentification Key (FCM Token). Please, refer to section "Authentification" of the FCM documentation, at https=//firebase.google.com/docs

使用Postman扩展名从Chrome浏览器开始工作。

Using the Postman extension from Chrome, it works.

这是代码:

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  IdHTTP, IdIOHandler, IdIOHandlerStream,
  IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL, IdSSLOpenSSLHeaders_Static,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo,
  IdGlobal, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdServerIOHandler, IdCoderMIME;
begin
  try
    IdIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(nil);
    IdIOHandler.ReadTimeout := IdTimeoutInfinite;
    IdIOHandler.ConnectTimeout := IdTimeoutInfinite;
    IdHTTP := TIdHTTP.Create(nil);
    try
      idHttp.Request.Clear;
      idHttp.Request.CustomHeaders.Clear;
      idHttp.Request.ContentType := 'application/json';
      idhttp.Request.Charset := 'UTF-8';
      IdHTTP.IOHandler := IdIOHandler;
      IdHTTP.ReadTimeout := IdTimeoutInfinite;
      IdHTTP.Request.Connection := 'Keep-Alive';
      IdIOHandler.SSLOptions.Method := sslvSSLv23;
      IdHTTP.Request.Method := 'POST';
      IdHTTP.Request.CustomHeaders.Values['Authorization:key'] := 'AAAAYsnMbsY:APA91bEjYZK-xxxxxxx......';
      jsonString := '{"to" : "APA91bFJSdGW_yrX7p_TNKZ4k0OpdXTQM6xdd7BUsslk6zSvZlBmoAnfvyX-nBm4DYY-xxxxx......",' +
                    '"data" : {' +
                    '"Nick" : "Teste de Push",' +
                    '"body" : "Corpo do push",' +
                    '"Room" : "Apenas um teste"' +
                    '},}';
      JsonToSend := TStringStream.Create(jsonString);
      try
        response := IdHTTP.Post('https://fcm.googleapis.com/fcm/send', JsonToSend);
        response := response.Replace(Char(#10), '');
      except
        on E:EIdHTTPProtocolException do
        memo1.Lines.Insert(0, e.ErrorMessage);
      end;
      memo1.Lines.Insert(0, response);
    finally
      IdHTTP.Free;
    end;
  finally
    IdIOHandler.Free;
  end;
end;


推荐答案

您的Delphi代码未分配身份验证请求标头正确。您需要更改以下内容:

Your Delphi code is not assigning the Authentication request header correctly. You need to change this:

IdHTTP.Request.CustomHeaders.Values['Authorization:key'] := 'AAAAYsnMbsY:APA91bEjYZK-xxxxxxx......';

为此:

IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'key=AAAAYsnMbsY:APA91bEjYZK-xxxxxxx......';

您应设置 IdHTTP.Request.BasicAuthentication

You should set the IdHTTP.Request.BasicAuthentication property to false as well.

除此之外,由于将 Request.Charset 属性设置为UTF -8,您应该构造 TStringStream 来匹配:

Aside from that, since you are setting the Request.Charset property to UTF-8, you should construct the TStringStream to match:

JsonToSend := TStringStream.Create(jsonString, TEncoding.UTF8);

话虽如此,请尝试以下代码:

With that said, try the following code:

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
  FMX.Controls.Presentation, FMX.StdCtrls, FMX.ScrollBox, FMX.Memo,
  IdGlobal, IdBaseComponent, IdComponent, IdTCPConnection, IdTCPClient,
  IdHTTP, IdIOHandler, IdIOHandlerSocket, IdIOHandlerStack, IdSSL, IdSSLOpenSSL
  {$IFDEF IOS}
  , IdSSLOpenSSLHeaders_Static
  {$ENDIF}
  ;

...

begin
  IdHTTP := TIdHTTP.Create(nil);
  try
    IdIOHandler := TIdSSLIOHandlerSocketOpenSSL.Create(IdHTTP);
    IdIOHandler.SSLOptions.Method := sslvSSLv23;
    IdHTTP.IOHandler := IdIOHandler;

    IdHTTP.Request.ContentType := 'application/json';
    IdHTTP.Request.Charset := 'UTF-8';
    IdHTTP.Request.CustomHeaders.Values['Authorization'] := 'key=AAAAYsnMbsY:APA91bEjYZK-xxxxxxx......';

    jsonString := '{"to" : "APA91bFJSdGW_yrX7p_TNKZ4k0OpdXTQM6xdd7BUsslk6zSvZlBmoAnfvyX-nBm4DYY-xxxxx......",' +
                   '"data" : {' +
                   '"Nick" : "Teste de Push",' +
                   '"body" : "Corpo do push",' +
                   '"Room" : "Apenas um teste"' +
                   '},}';
    JsonToSend := TStringStream.Create(jsonString, TEncoding.UTF8);
    try
      response := IdHTTP.Post('https://fcm.googleapis.com/fcm/send', JsonToSend);
      response := response.Replace(#10, '');
    except
      on E: EIdHTTPProtocolException do
        response := e.ErrorMessage;
    end;
    Memo1.Lines.Insert(0, response);
  finally
    IdHTTP.Free;
  end;
end;

这篇关于Delphi-Google Firebase-HTTP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆