Question Details

Answered
getting an error for LINE Message API hit using c#

Hi Team, I am trying to hit the LINE message api https://api.line.me/v2/bot/message/push for message push using c#. it gives an error as "The remote server returned an error: (401) Unauthorized." please give me some solutions. i have posted the code here. thanks

					 private string SendRequest(Uri uri, byte[] jsonDataBytes, string contentType, string method, string authorization)
    {
        WebRequest req = WebRequest.Create(uri);
        req.ContentType = contentType;
        req.Method = method;
        req.ContentLength = jsonDataBytes.Length;
        req.Headers.Add("Authorization", authorization);

        var stream = req.GetRequestStream();
        stream.Write(jsonDataBytes, 0, jsonDataBytes.Length);
        stream.Close();

        WebResponse response = req.GetResponse();
        MessageBox.Show(((HttpWebResponse)response).StatusDescription);
        stream = response.GetResponseStream();
        StreamReader reader = new StreamReader(stream);
        string responseFromServer = reader.ReadToEnd();

        return responseFromServer;
    }

    private void button1_Click(object sender, EventArgs e)
    {
        string userId = "U9de0102ab382c20d995147ac87c78403";
        //string userId = txt_UID.Text;
        string word = txt_msg.Text;
        string mystr = "https://api.line.me/v2/bot/message/push";
        string apikey = "Bwmt0w4rBtcMivriy2VyH4VzOykZQ22Dkm/xk4bQ2GzUwO2tFw6hzQBgFKvRXpiDZOsEp6dLo6hyz6QbFPYSynIicuUoJrG4Jxg7yZRl9SUpmPg20c9N6wBPCQPOEoaC762c7sRF5nOD+bx1gEjmRQdB04t89/1O/w1cDnyilFU=";
        //JSON
        JObject obj = new JObject();
        obj.Add("to", userId);
        JArray msg = new JArray();
        JObject m1 = new JObject();
        m1.Add("type", "text");
        m1.Add("text", word);
        msg.Add(m1);
        obj.Add("messages", msg);
        string obj_S = JsonConvert.SerializeObject(obj);
        txt_JSON.Text = obj_S;
        //POST
        Uri myuri = new Uri(mystr);
        var data = Encoding.UTF8.GetBytes(obj_S);
        SendRequest(myuri, data, "application/json", "POST", apikey);
    }
0
Edited: 1 years ago
Profile picture
Ramasamy

Answers

1
Profile picture
Mick Satana
Posted: 1 years ago

It seem authorization not correct

You forgot to prepend "Bearer " in front of your token

it should be "Bearer Bwmt0w4rBtcMivriy2VyH4VzOykZQ22Dkm/xk4bQ2GzUwO2tFw6hzQBgFKvRXpiDZOsEp6dLo6hyz6QbFPYSynIicuUoJrG4Jxg7yZRl9SUpmPg20c9N6wBPCQPOEoaC762c7sRF5nOD+bx1gEjmRQdB04t89/1O/w1cDnyilFU="

Are you sure? question.vm