I am using Baidu Push API to send push notification messages to my device. But I am getting Authentication failure in the response. I tried to contact Baidu support but they just don't reply.
Error response: { "request_id": 2727123347, "error_code": 30603, "error_msg": "Authentication Failed" }
Following are my API params details
----------POST Url----------
https://api.tuisong.baidu.com/rest/3.0/push/all
----------Headers----------
Content-Type = application/x-www-form-urlencoded;charset=utf-8
User-Agent = BCCS_SDK/3.0 (Darwin; Darwin Kernel Version 14.0.0: Fri Sep 19 00:26:44 PDT 2014; root:xnu-2782.1.97~2/RELEASE_X86_64; x86_64) PHP/5.6.3 (Baidu Push Server SDK V3.0.0 and so on..) cli/Unknown ZEND/2.6.0
----------Form Data----------
apikey = XXXXXXXXXX
expires = 1593494000
msg = %7B%22title%22%3A%22hello%22%2C%22description%22%3A%22hello%20world%22%2C%22notification_builder_id%22%3A0%2C%22notification_basic_style%22%3A7%2C%22open_type%22%3A0%2C%22url%22%3A%22http%3A%2F%2Fdeveloper.baidu.com%22%2C%22pkg_content%22%3A%22%22%2C%22custom_content%22%3A%7B%22key%22%3A%22value%22%7D%2C%22target_channel_id%22%3A%22%22%7D
timestamp = 1593493655
secret_key = XXXXXXXXXXX
sign = f7a7b96457162795c491d993a1b821a7
I have used below references for preparing params and making API call.
Please help me to fix this. Thanks in advance.
The Baidu Rest Api is very sensitive regarding the encoding of the signature string before you hash it. See http://push.baidu.com/doc/restapi/sdk_developer -> Signature algorithm section. You have to use the exact
UrlEncode()algorithm.Doing extensive trial and error I found the C# method
System.Net.WebUtility.UrlEncode(raw)which uses the encoding expected by the Rest API. Make sure that the method you are using in the language of your choice encodes as follows:raw:
{"Hello":"Stackoverflow @ https://stackoverflow.com"}encoded:
%7B%22Hello%22%3A%22Stackoverflow+%40+https%3A%2F%2Fstackoverflow.com%22%7DNote:
' '(space) encoded as'+'and not%20%7B) use capital letters