C# 无法使用Firebase REST API发送批处理消息

C# 无法使用Firebase REST API发送批处理消息,c#,firebase-cloud-messaging,dotnet-httpclient,C#,Firebase Cloud Messaging,Dotnet Httpclient,我正在试用RESTAPI在Firebase云消息传递中发送批处理消息。我在C#中准备了一个多部分HTTP请求: 上面的多播字段的字符串值是一个HTTP内容,类似于中提供的内容: Firebase服务器返回错误的请求-400,错误消息为:“无法分析批处理请求,错误:0项。收到的批处理正文:--subrequest\u boundary-->,这表示Firebase直接使用终止的--subrequest\u boundary-->处理内容 问题的原因可能是什么?魔鬼在构建内容的细节中。您不能简单地创

我正在试用RESTAPI在Firebase云消息传递中发送批处理消息。我在C#中准备了一个多部分HTTP请求:

上面的
多播
字段的字符串值是一个HTTP内容,类似于中提供的内容:

Firebase服务器返回错误的请求-400,错误消息为:
“无法分析批处理请求,错误:0项。收到的批处理正文:--subrequest\u boundary-->
,这表示Firebase直接使用终止的
--subrequest\u boundary-->处理内容


问题的原因可能是什么?

魔鬼在构建
内容的细节中。您不能简单地创建一个与该有效负载匹配的字符串,因为它的某些部分,如标题和边界,在技术上不被
HttpClient
堆栈视为主体的一部分


如果您确实需要使用原始的
HttpClient
,请查看。但我怀疑,一旦你意识到这有多么麻烦,你就会得出结论,你最好使用更高级别的SDK,例如,在你链接到的同一文档中提到的,并在引擎盖下使用Google的.NET客户端。

尝试更改你的代码

request.Content.Headers.TryAddWithoutValidation("Content-Type", "multipart/mixed; boundary=--subrequest_boundary");


昨天,我需要编写bash脚本来发送bath FCM notifaction,并查看您的代码@Ugur,谢谢。 现在它起作用了,你需要改变

内容类型“,”多部分/混合;边界=--子请求\u边界

内容类型“,”多部分/混合;边界=子请求\u边界

剧本

#!/bin/bash
curl \
      -X POST \
      -H "Authorization: Bearer [token_auth]" \
      -H "Content-Type: multipart/mixed; boundary=subrequest_boundary" \
      --data-binary @test2.txt \
      https://fcm.googleapis.com/batch
和test2.txt,示例发送2通知

--子请求\u边界 内容类型:应用程序/http 内容传输编码:二进制 授权:持票人[代币授权]

POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }

    }
}

--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]

POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }
    }
}

--subrequest_boundary--
  • 将[project\u name\u firebase]更改为firebase中的项目名称 控制台示例:project_3323
  • 将[token_device]更改为token目标设备
  • 使用您的google auth凭据令牌更改[token_auth]
通知配置:

获取[token_auth]

POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }

    }
}

--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]

POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }
    }
}

--subrequest_boundary--
  • 爪哇:
  • 植物:
  • 卷曲:
get[token\u auth],getToken.sh的代码

#!/bin/bash
client_email="[email_project_firebase]"
service="https://oauth2.googleapis.com/token"
time=3600
scope="https://www.googleapis.com/auth/cloud-platform"
private_key=$(echo -ne "[private_key]")

datenow="$(date +%s)"
expired="$(( datenow + time ))";

header='{"alg": "RS256","typ": "JWT"}'
payload='{"iss": "'$client_email'","scope": "'$scope'","aud": "'$service'","exp": '$expired',"iat": '$datenow'}'
HEADEREnc=$( echo -n "${header}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
PAYLOADEnc=$( echo -n "${payload}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )

data="${HEADEREnc}"."${PAYLOADEnc}"
signature=$( openssl dgst -sha256 -sign <(echo -n "${private_key}") <(echo -n "${data}") | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )

JWT="${data}"."${signature}"
jsonDataToken=$(curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='$JWT https://oauth2.googleapis.com/token)
tokenAuth=$(echo -n $jsonDataToken |grep -Po '"access_token":.*?[^\\]"'|cut -d ':' -f2 |sed 's/"//g')
echo $tokenAuth
#/bin/bash
客户电子邮件=“[email\u project\u firebase]”
服务=”https://oauth2.googleapis.com/token"
时间=3600
范围=”https://www.googleapis.com/auth/cloud-platform"
私钥=$(echo-ne“[private\u key]”)
datenow=“$(日期+%s)”
expired=“$((datenow+time))”;
header='{“alg”:“RS256”,“typ”:“JWT”}'
有效负载=“{”iss:“$client_email”“”,“scope:“$scope”“,”aud:“$service”“,”exp:“$expired”“,”iat:“$datenow'}”
headernc=$(echo-n“${header}”| opensslbase64 | tr-d'='| tr'/+'''.-'| tr-d'\n')
PAYLOADEnc=$(echo-n“${payload}”| openssl base64 | tr-d'='| tr'/+'''|tr-'\n')
data=“${headernc}”。${PAYLOADEnc}”
签名=$(openssl dgst-sha256-签署项目设置或单击齿轮图标->帐户服务->单击按钮“创建新密钥”

列出其他服务的范围

我的回答有用吗?
POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }

    }
}

--subrequest_boundary
Content-Type: application/http
Content-Transfer-Encoding: binary
Authorization: Bearer [token_auth]

POST /v1/projects/[project_name_firebase]/messages:send
Content-Type: application/json
accept: application/json

{
    "message":{
    "token":"[token_device]",
        "notification":{ 
            "title":"FCM Message",
            "body":"This is an FCM notification message to device 0!",
        }
    }
}

--subrequest_boundary--
#!/bin/bash
client_email="[email_project_firebase]"
service="https://oauth2.googleapis.com/token"
time=3600
scope="https://www.googleapis.com/auth/cloud-platform"
private_key=$(echo -ne "[private_key]")

datenow="$(date +%s)"
expired="$(( datenow + time ))";

header='{"alg": "RS256","typ": "JWT"}'
payload='{"iss": "'$client_email'","scope": "'$scope'","aud": "'$service'","exp": '$expired',"iat": '$datenow'}'
HEADEREnc=$( echo -n "${header}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )
PAYLOADEnc=$( echo -n "${payload}" | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )

data="${HEADEREnc}"."${PAYLOADEnc}"
signature=$( openssl dgst -sha256 -sign <(echo -n "${private_key}") <(echo -n "${data}") | openssl base64 | tr -d '=' | tr '/+' '_-' | tr -d '\n' )

JWT="${data}"."${signature}"
jsonDataToken=$(curl -d 'grant_type=urn%3Aietf%3Aparams%3Aoauth%3Agrant-type%3Ajwt-bearer&assertion='$JWT https://oauth2.googleapis.com/token)
tokenAuth=$(echo -n $jsonDataToken |grep -Po '"access_token":.*?[^\\]"'|cut -d ':' -f2 |sed 's/"//g')
echo $tokenAuth