Monday, February 27, 2023

Generate Token using x++

 Generate Token using x++.


using System.IO;
using System.Net;
using System.Text;
using Microsoft.WindowsAzure.Storage;
using Microsoft.WindowsAzure.Storage.File;
using Microsoft.IdentityModel.Clients.ActiveDirectory;
using System.Threading.Tasks;

public class Authenticationtoken
{
    public void TokenGen()
    {
        WebException                            exception;
        System.Net.HttpWebRequest               webrequest;
        CLRObject                   		webResponse =null;
        System.Exception            		ex;
        #OCCRetryCount
		
        System.Net.HttpWebResponse response;
        str response1;
        str               	byteStr;
        System.Byte[]           byteArray;
        System.IO.Stream        stream;
        System.IO.Stream        dataStream;
		System.Text.Encoding    utf8;
		
	//Using username and password for Authentication
        byteStr = strfmt('%1:%2', "Username", "PassWord");
		
        webrequest = System.Net.WebRequest::Create("URL") as System.Net.HttpWebRequest;
        
	// Parameters
        webRequest.Method           = 'POST';// POST / Get
        webRequest.KeepAlive        = true;
        webRequest.ContentType      = "application/json";  
        webRequest.ContentLength    = 0;
        //webrequest.Host             ="";
        // webrequest.UserAgent        ="";
        //webrequest.Connection       ="";
        webRequest.Timeout          = maxInt();
		
	//Headers
        System.Net.WebHeaderCollection headers = new System.Net.WebHeaderCollection();
        headers = webRequest.Headers;
		
	// Encoding
        utf8       = System.Text.Encoding::get_UTF8();
        byteArray  = utf8.GetBytes(byteStr);
        byteStr    = System.Convert::ToBase64String(byteArray);
		
        headers.Add("Authorization", 'Basic ' + byteStr);
        //headers.Add("Username", "123");

        if (webRequest != null)
        {
            webResponse = webRequest.GetResponse();

            using (StreamReader streamReader = new StreamReader(webResponse.GetResponseStream()))
            {
               response1 = streamReader.readToEnd();
            }

            webResponse.Close();
        }
        else
        {
            throw new System.Exception("Please provide valid URL");
        }
		
        return Authenticationtoken::deserialize(response1);
    }

    public static void deserialize(str  _json)
    {
        AttendanceAuthenticationContract        attendanceAuthenticationContract;
        
        // List
        //attendanceAuthenticationContract = FormJsonSerializer::deserializeCollection(classNum(AttendanceAuthenticationContract),
        //    _json, Types::Class, classStr(AttendanceAuthenticationContract));

        attendanceAuthenticationContract = FormJsonSerializer::deserializeObject(classNum(AttendanceAuthenticationContract),
            _json);

	//Token
        info(attendanceAuthenticationContract.parmBearerToken());
		
	---------------------or--------------------------------
	Map map = RetailCommonWebAPI::getMapFromJsonString(_json);

        if (map && map.exists('access_token'))
        {
            info(map.lookup('access_token'));
        }
    }

}



Keep Daxing!!


No comments:

Post a Comment