How can i log in. ?
- To login You need to provide {Key} in authentication header of request. - To Login you need to call /api/Authenticate/
Jquery | C# | VB.Net |
function AuthenticateKeyAndGetToken() { var _authenticateUri = "{Server}/api/Authenticate"; $.ajax({ url: _authenticateUri, type: 'GET', beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', "bearer " + {key}); }, data: {}, success: function (data) { '// Get Your Token Here var Token = data; }, error: function (xhr, ajaxOptions, thrownError) { '// Show Error, if you get any. alert(thrownError); }, }); } Function GetAccessToken() As String Dim _token As String = String.Empty 'Your Access Token Try Dim _apiServer ' API Server Dim _key ' Your {Key} based upon your credential. Dim _uri = String.Format("{0}/api/Authenticate", _apiServer) Dim webrequest As HttpWebRequest = DirectCast(Net.WebRequest.Create(_uri), HttpWebRequest) webrequest.Headers.Add("Authorization", "bearer " + _key) Dim webresponse As HttpWebResponse = webrequest.GetResponse() Dim enc As Encoding = System.Text.Encoding.GetEncoding(1252) Dim loResponseStream As New StreamReader(webresponse.GetResponseStream(), enc) Dim Response As String = loResponseStream.ReadToEnd() loResponseStream.Close() webresponse.Close() _token = Response Catch wex As WebException Throw New HttpException(CInt(HttpStatusCode.BadRequest), DirectCast(wex.Response, HttpWebResponse).StatusDescription) Catch ex As Exception Dim responseMessage As New HttpResponseMessage(HttpStatusCode.ExpectationFailed) responseMessage.ReasonPhrase = ex.Message.ToString + "Details: " + ex.InnerException.ToString Throw New Web.Http.HttpResponseException(responseMessage) End Try Return _token End Function public string GetAccessToken() { string _token = string.Empty; //Your Access Token try { var _apiServer = null; // API Server var _key = null; // Your {Key} based upon your credential. var _uri = string.Format("{0}/api/Authenticate", _apiServer); HttpWebRequest webrequest = (HttpWebRequest)Net.WebRequest.Create(_uri); webrequest.Headers.Add("Authorization", "bearer " + _key); HttpWebResponse webresponse = webrequest.GetResponse(); Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); string Response = loResponseStream.ReadToEnd(); loResponseStream.Close(); webresponse.Close(); _token = Response; } catch (WebException wex) { throw new HttpException(Convert.ToInt32(HttpStatusCode.BadRequest), ((HttpWebResponse)wex.Response).StatusDescription); } catch (Exception ex) { HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed); responseMessage.ReasonPhrase = ex.Message.ToString + "Details: " + ex.InnerException.ToString; throw new Web.Http.HttpResponseException(responseMessage); } return _token; } |
How can i use API Call. ?
- You need to provide {Token} in the authentication header of request every time you made an call.
- {Token} is valid for only an hour.
- Once your {Token} get expired, you need to login again.
- You need to provide "Accept" as "application/xml" or "application/json" for required format as a result.
Jquery | C# | VB.Net |
function getdata() { var server = ""; // Your Server var _apiCall = ""; // API Call to make e.g. '/api/Get_Transactions' var _accept=""; // application/xml OR application/json var _accesstoken =""; // Access Token that you get after calling "{Server}/api/Authenticate" API. $.ajax({ url: _apiCall, type: 'GET', headers: { 'Accept': _accept }, beforeSend: function (xhr) { xhr.setRequestHeader('Authorization', "bearer " + _accesstoken); }, data: {}, success: function (data) { var _returndata = ""; // check weather return data is XML or JSON if (jQuery.isXMLDoc(data)) { // write your code here.. you will get data as a XML Document. } else { // write your code here.. you will get data as a JSON string. } }, error: function (xhr, ajaxOptions, thrownError) { debugger; alert(thrownError); } }); } public void GetAPIData() { try { // provide Server string _apiServer = string.Empty; // provide API Call string _apiRequest = string.Empty; // e.g "/api/Get_Transactions" //provide Output Type string _outputType = string.Empty; // this will be "application/xml" OR "application/json" dynamic _token = string.Empty; //Access Token that you get after calling "{Server}/api/Authenticate" API HttpWebRequest webrequest = (HttpWebRequest)System.Net.WebRequest.Create(_apiServer + _apiRequest); webrequest.ContentType = _outputType; webrequest.Accept = _outputType; webrequest.Headers.Add("Authorization", "bearer " + _token); HttpWebResponse webresponse = webrequest.GetResponse(); Encoding enc = System.Text.Encoding.GetEncoding(1252); StreamReader loResponseStream = new StreamReader(webresponse.GetResponseStream(), enc); string Response = loResponseStream.ReadToEnd(); loResponseStream.Close(); webresponse.Close(); if (_outputType == "application/xml") { // Write your code here for XML } else { // Write your code here for JSON } } catch (WebException wex) { throw new HttpException(Convert.ToInt32(HttpStatusCode.BadRequest), ((HttpWebResponse)wex.Response).StatusDescription); } catch (Exception ex) { HttpResponseMessage responseMessage = new HttpResponseMessage(HttpStatusCode.ExpectationFailed); responseMessage.ReasonPhrase = ex.Message.ToString() + "Details: " + ex.InnerException.ToString(); throw new Web.Http.HttpResponseException(responseMessage); } } Sub GetAPIData() Try ' provide Server Dim _apiServer = String.Empty ' provide API Call Dim _apiRequest = String.Empty ' e.g "/api/Get_Transactions" 'provide Output Type Dim _outputType = String.Empty ' this will be "application/xml" OR "application/json" Dim _token = String.Empty 'Access Token that you get after calling "{Server}/api/Authenticate" API Dim webrequest As HttpWebRequest = DirectCast(Net.WebRequest.Create(_apiServer + _apiRequest), HttpWebRequest) webrequest.ContentType = _outputType webrequest.Accept = _outputType webrequest.Headers.Add("Authorization", "bearer " + _token) Dim webresponse As HttpWebResponse = webrequest.GetResponse() Dim enc As Encoding = System.Text.Encoding.GetEncoding(1252) Dim loResponseStream As New StreamReader(webresponse.GetResponseStream(), enc) Dim Response As String = loResponseStream.ReadToEnd() loResponseStream.Close() webresponse.Close() If _outputType = "application/xml" Then ' Write your code here for XML Else ' Write your code here for JSON End If Catch wex As WebException Throw New HttpException(CInt(HttpStatusCode.BadRequest), DirectCast(wex.Response, HttpWebResponse).StatusDescription) Catch ex As Exception Dim responseMessage As New HttpResponseMessage(HttpStatusCode.ExpectationFailed) responseMessage.ReasonPhrase = ex.Message.ToString + "Details: " + ex.InnerException.ToString Throw New Web.Http.HttpResponseException(responseMessage) End Try End Sub |