Go to All Forums

Getting monitor data using oAuth token in JSONP and/or c# WebClient?

I am doing some work utilizing the API and saw the notice that Site24x7 is migrating to oAuth 2.0.

 

Previously, I was able to successfully fetch monitor data using JSONP (and still can, however this is being deprecated in a few months). Following the documentation, I was able to create a new "Self Client", refresh token, and access token, however I haven't been able to use the new access tokens successfully.

First:

Will JSONP still be supported? The documentation states:

Note: Access tokens cannot be passed in the request param.

There's no way that I know of to pass parameters in POST that are part of a script tag, so I'm not sure if JSONP is possible with that limitation.

 

Second:

I don't have access to curl which is what all the other API examples use. With c#, I am not having success (usually 403 errors) using the new oAuth token with approaches similar to this one:

var client = new WebClient();

client.Headers.Add("Authorization", "Zoho-oauthtoken 1000.972f24********0e1fc");

string responseString = client.DownloadString("www.site24x7.com/api/monitors");

 

Any guidance / working c# samples would be greatly appreciated.

 

Like (3) Reply
Replies (7)

Dear Warren,

The 400+ error code may due to the fact that wrong METHOD is used. Can you share your OAuth source for us to check? 

   Here is the C# version of the OAuth APIs I tried. The below snippet is for fetching the current_status of all monitors. Here I have passed the AccessToken generated from the first grant token request. 

try
{
    Site24x7AuthToken token = Site24x7OAuth.PersistenceHandlerInstance.OAuthToken;
    string url = Site24x7ReportsApiGenerator.Site24x7CurrentStatusUrl;
Site24x7HttpConnector conn = new Site24x7HttpConnector() { Url = url };
Site24x7QueryParams qparams = new Site24x7QueryParams();

conn.AddParam("status_required", qparams.IsStatusRequired.ToString());
conn.AddHeader("Authorization", "Zoho-oauthtoken " + token.AccessToken);
return conn.Get();
}
catch(Exception ex)
{
throw new Exception(ex.Message, ex.InnerException);
}

In this snippet I just call the get request with the required headers
try
{
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
request.UserAgent = "Mozilla/5.0";

if (RequestHeaders != null && RequestHeaders.Count != 0)
{
foreach (KeyValuePair<string, string> header in RequestHeaders)
{
request.Headers[header.Key] = header.Value;
}
}

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = "";
using (StreamReader reader = new StreamReader(response.GetResponseStream()))
{
responseString = reader.ReadToEnd();
}
return responseString;
}
catch (WebException e)
{
throw new Exception(e.ToString());
}

The full code is available in the zip file attached.

PS: The attached code was compiled in .NET 4.6.1 in Visual studio 2017. The code does not use async methods.

-Jasper
Product Manager, Site24x7


 

Attachments
Site24x7.Api.Client.zip
Size: 457.16 KB
Like (0) Reply

Jasper,

Thank you for your response and for attaching a code sample. I believe I have reproduced your approach and am still getting a 403 Forbidden response. Here is my code:

string token = "1000.fc88e7cb155 [...]";
string url = "www.site24x7.com/api/current_status";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0";
request.Headers["Authorization"] = "Zoho-oauthtoken "+token;
request.Accept = "application/json; version=2.0";
request.ContentType = "application/x-www-form-urlencoded";
string postData = "status_required=true";
var data = Encoding.UTF8.GetBytes(postData);

request.ContentLength = data.Length;
request.Method = "POST";
using (var dataStream = request.GetRequestStream()){
    dataStream.Write(data, 0, data.Length);
}
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = "";
using (StreamReader reader = new StreamReader(response.GetResponseStream())){
	responseString = reader.ReadToEnd();
}
return responseString;

The result is the same using either an access token with Site24x7.Reports.Read scope, or Site24x7.Admin.All scope. To obtain the access token, I just use a simple form:

<form method="POST" action="accounts.zoho.com/oauth/v2/token">

 <input name="client_id" value="1000.QNIGZ[...]" type="hidden" />

  <input name="client_secret" value="3f07ac[...]" type="hidden"/>

  <input name="refresh_token" value="1000.1f56c6[...]" type="hidden"/>

  <input name="grant_type" value="refresh_token" type="hidden"/>

  <input type="submit" value="Generate token"/>

</form>

 

 

 

Like (0) Reply

Hey Warren, 

  You are using the METHOD as POST for current status api. It should be GET.

Can you remove the below line (by default the request METHOD is a GET) from your code and do a request for the current_status api?
request.Method = "POST";

-Jasper

 

Like (0) Reply

Jasper,

I am using the POST method for current status because that is what is being done in the code sample you provided. Regardless, I removed the requested line (and other related POST lines that failed if the method was not POST) and still received the 403 response. Below is the updated code (a newly generated access token was used so it should not be a matter of the token expiring).

string token = "1000.755 [...]";
string url = "www.site24x7.com/api/current_status";
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
request.UserAgent = "Mozilla/5.0";
request.Headers["Authorization"] = "Zoho-oauthtoken "+token;
request.Accept = "application/json; version=2.0";

HttpWebResponse response = (HttpWebResponse)request.GetResponse();
string responseString = "";
using (StreamReader reader = new StreamReader(response.GetResponseStream())){
	responseString = reader.ReadToEnd();
}
return responseString;
Like (0) Reply

As an additional note, I tried downloading curl and the resulting error was:

Invalid value passed for authtoken

 

Not sure where I can go from here, I am using the temporary access token from my permanent refresh token, and prepending it with "Zoho-oauthtoken " as outlined in the examples.

Like (0) Reply

Dear Warren,

  Can we have a support session with you to figure it out together? Please raise a ticket with us in support@site24x7.com. Please mention a time and we'll get in touch with you. I'll post a summary here after we sort things out for you.

-Jasper

Like (0) Reply

Is there an API using which Status of a Monitor can be updated?

Like (0) Reply

Was this post helpful?