C#

This tutorial provides you with a step-by-step walkthrough on how to send a message using the Live Link 365 365 API.

This is not a production-ready application. Please take your time to enhance it for production so that it meets your specific business requirements.

Steps

Code

keyboard_arrow_down
  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
using System;
using System.Net;
using System.Text;

// _Step_1
using Newtonsoft.Json;
using RestSharp;

namespace HelloWorld
{

    // _Step_2
    static class Globals
    {
        public const String baseUrl = "https://livelink.sapdigitalinterconnect.com/api";
        public const String appKey = "";
        public const String appSecret = "";
        public const String defaultOrigin = "";
        public const WebProxy globalProxy = null;
    }

    public class HelloWorld
    {
        // _Step_3
        public static String GetAccessToken(String key = Globals.appKey, String secret = Globals.appSecret)
        {
            Console.WriteLine("...... executing authorization request with key: " + key);

            String oAuthUrl = Globals.baseUrl + "/oauth/token";
            String bearer = Convert.ToBase64String(Encoding.UTF8.GetBytes(key + ":" + secret));
            RestClient client = new RestClient(oAuthUrl);
            if (Globals.globalProxy != null) {
                client.Proxy = Globals.globalProxy;
                client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }
            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("content-type", "application/x-www-form-urlencoded");
            request.AddHeader("authorization", "Basic " + bearer);
            request.AddParameter("application/x-www-form-urlencoded", "grant_type=client_credentials", ParameterType.RequestBody);

            IRestResponse<oAuthToken> response = client.Execute<oAuthToken>(request);

            if (response.StatusCode != HttpStatusCode.OK) {
                Console.WriteLine("..... Error: " + response.StatusDescription);
                return "";
            }

            String accessToken = response.Data.access_token;
            Console.WriteLine("..... authorization request successfull access token: " + accessToken);
            return accessToken;
        }

        // _Step_4
        public static void sendSMS(String message, String destination)
        {
            Console.WriteLine("..... sending message");

            String messageUrl = Globals.baseUrl + "/v2/sms";
            Object messagePayload = new {
                message = message,
                origin =  Globals.defaultOrigin,
                destination = destination
            };
            String messageData = JsonConvert.SerializeObject(messagePayload);
            String bearer = GetAccessToken();
            if (String.IsNullOrEmpty(bearer)) {
                return;
            }
            RestClient client = new RestClient(messageUrl);
            if (Globals.globalProxy != null) {
                client.Proxy = Globals.globalProxy;
                client.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
            }

            RestRequest request = new RestRequest(Method.POST);
            request.AddHeader("authorization", "Bearer " + bearer);
            request.AddHeader("content-type", "application/json");
            request.AddParameter("application/json", messageData, ParameterType.RequestBody);

            IRestResponse response = client.Execute(request);

            if (response.StatusCode != HttpStatusCode.OK) {
                Console.WriteLine("..... failed to send message.");
                Console.WriteLine("..... Error: " + response.ErrorMessage);
                return;
            }
            Console.WriteLine("..... Message sent successfully");
            return;
        }

        // _Step_5
        public static void Main(string[] args)
        {
            Console.Clear();
            Console.Write("What do you want to say(text message): ");
            String message = Console.ReadLine();
            Console.Write("Who do you want to send to(phone number): ");//Phone numbers have to be in E.164 format.
            String destination = Console.ReadLine();
            Console.WriteLine("****************************************");
            Console.WriteLine("Sending: " + message);
            Console.WriteLine("To: " + destination);
            sendSMS(message, destination);
        }
    }
}