I have created one web app for that I have generated access token via OAuth2.0 My app have all read write permissions but I am not able get response. Same has been working with Merchant's static access token generated from Merchant settings. So Any extra information need to be add in web app. Because Same I have tested with Android app and it is working fine with same App configuration like I have for Web App.
Hello Team, Below is my code snippet. It is working code for production but when I change it to sandbox by changing end point url got response as No content.
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.IO; using System.Net; using System.Text; using Newtonsoft.Json; using System.Data; using System.Xml; using System.Xml.Linq;
public partial class Login : System.Web.UI.Page { public string strMerchantID { get { return Convert.ToString(ViewState["MerchantID"]); } set { ViewState["MerchantID"] = value; } } public string strEmployeeID { get { return Convert.ToString(ViewState["EmployeeID"]); } set { ViewState["EmployeeID"] = value; } } public string strClientID { get { return Convert.ToString(ViewState["ClientID"]); } set { ViewState["ClientID"] = value; } } public string strAccessToken { get { return Convert.ToString(ViewState["AccessToken"]); } set { ViewState["AccessToken"] = value; } } public string EndPoint { get; set; } public HttpVerb Method { get; set; } private string ContentType = "application/json"; public string ContentType { get { return _ContentType; } set { _ContentType = value; } } public string PostData { get; set; } protected void PageLoad(object sender, EventArgs e) { if (!Page.IsPostBack) { strMerchantID = Request.QueryString["merchantid"]; strEmployeeID = Request.QueryString["employeeid"]; strClientID = Request.QueryString["clientid"]; //btnFirstLoadClick(btnFirstLoad, null); } else { if (!string.IsNullOrEmpty(hdnAccessToken.Value) && Convert.ToString(hdnAccessToken.Value).Contains("#")) { string CompleteQueryString = hdnAccessToken.Value; strAccessToken = CompleteQueryString.Substring(CompleteQueryString.IndexOf("#"), CompleteQueryString.Length - CompleteQueryString.IndexOf("#")).Replace("#accesstoken=", ""); } } } public DataTable XElementToDataTable(XElement x) { DataTable dtable = new DataTable(); XElement setup = (from p in x.Descendants() select p).First(); // build your DataTable foreach (XElement xe in setup.Descendants()) dtable.Columns.Add(new DataColumn(xe.Name.ToString(), typeof(string))); // add columns to your dt var all = from p in x.Descendants(setup.Name.ToString()) select p; foreach (XElement xe in all) { DataRow dr = dtable.NewRow(); foreach (XElement xe2 in xe.Descendants()) dr[xe2.Name.ToString()] = xe2.Value; //add in the values dtable.Rows.Add(dr); } return dtable; } protected void btnGetItemsClick(object sender, EventArgs e) { try { //Manoj (Production) var client = new RestClient(EndPoint, Method, PostData); client.EndPoint = @"https://api.clover.com/v3/merchants/" + strMerchantID + "/"; //@"http:\myRestService.com\api\"; ; client.Method = HttpVerb.GET; client.ContentType = ContentType; client.PostData = ""; var json = client.MakeRequest("items", strAccessToken); json = "{\"tems\":" + json + "}"; XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json); XElement xRoot ; using (XmlNodeReader nodeReader = new XmlNodeReader(doc)) { nodeReader.MoveToContent(); xRoot = XElement.Load(nodeReader); } DataTable dt = XElementToDataTable(xRoot); grdItems.DataSource = dt; grdItems.DataBind(); } catch (Exception) { throw; } }
protected void btnGetOrders_Click(object sender, EventArgs e)
{
try
{
//Manoj (Production)
var client = new RestClient(EndPoint, Method, PostData);
client.EndPoint = @"https://api.clover.com/v3/merchants/" + strMerchantID + "/"; //@"http:\\myRestService.com\api\"; ;
client.Method = HttpVerb.GET;
client.ContentType = ContentType;
client.PostData = "";
var json = client.MakeRequest("orders", strAccessToken);
json = "{\"orders\":" + json + "}";
XmlDocument doc = (XmlDocument)JsonConvert.DeserializeXmlNode(json);
XElement xRoot;
using (XmlNodeReader nodeReader = new XmlNodeReader(doc))
{
nodeReader.MoveToContent();
xRoot = XElement.Load(nodeReader);
}
DataTable dt = XElementToDataTable(xRoot);
grdItems.DataSource = dt;
grdItems.DataBind();
}
catch (Exception)
{
throw;
}
}
protected void btnFirstLoad_Click(object sender, EventArgs e)
{
}
} public enum HttpVerb { GET, POST, PUT, DELETE } public class RestClient { public string EndPoint { get; set; } public HttpVerb Method { get; set; } public string ContentType { get; set; } public string PostData { get; set; }
public RestClient()
{
EndPoint = "";
Method = HttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint)
{
EndPoint = endpoint;
Method = HttpVerb.GET;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, HttpVerb method)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = "";
}
public RestClient(string endpoint, HttpVerb method, string postData)
{
EndPoint = endpoint;
Method = method;
ContentType = "application/json";
PostData = postData;
}
public string MakeRequest(string parameters,string AccessToken)
{
var request = (HttpWebRequest)WebRequest.Create(EndPoint + parameters);
request.Headers.Add("Authorization", "Bearer " + AccessToken);
request.Method = Method.ToString();
request.ContentLength = 0;
request.ContentType = ContentType;
if (!string.IsNullOrEmpty(PostData) && Method == HttpVerb.POST)
{
var encoding = new UTF8Encoding();
var bytes = Encoding.GetEncoding("iso-8859-1").GetBytes(PostData);
request.ContentLength = bytes.Length;
using (var writeStream = request.GetRequestStream())
{
writeStream.Write(bytes, 0, bytes.Length);
}
}
using (var response = (HttpWebResponse)request.GetResponse())
{
var responseValue = string.Empty;
if (response.StatusCode != HttpStatusCode.OK)
{
var message = String.Format("Request failed. Received HTTP {0}", response.StatusCode);
throw new ApplicationException(message);
}
// grab the response
using (var responseStream = response.GetResponseStream())
{
if (responseStream != null)
using (var reader = new StreamReader(responseStream))
{
responseValue = reader.ReadToEnd();
}
}
return responseValue;
}
}
}