I'm adding twitter integration to my companies web application but I'm hitting a snag on it.
The issue currently is with the callback url.
In twitter it seems that it wants to have a set value but the way our application is setup, each customer that access the site has their own url.
ideally I'd like to set the callback url on the fly, but I'm having a hard time finding any information on that.
EDIT:
Think I may need to add some more details here to better help you help me
The issue is I setup the callback url parameter to https://api.twitter.com/oauth/request_token but when I go to https://api.twitter.com/oauth/authorize to get permission, it will go to the callback setup on the api, not what I set from the previous request.
here is what my code looks like:
public string oAuthToken(string callbackUrl)
{
Uri oauthUrl = new Uri("https://api.twitter.com/oauth/request_token");
var oauthNonce = Convert.ToBase64String(new ASCIIEncoding().GetBytes(DateTime.Now.Ticks.ToString()));
var oauthTimestamp = this.GenerateTimeStamp();
var oauthSignature = "";
var authSignature = string.Format("oauth_callback=\"{0}\"&oauth_consumer_key=\"{1}\"&oauth_nonce=\"{2}\"&oauth_signature_method=\"{3}\"&oauth_timestamp=\"{4}\"&oauth_version=\"{5}\"",
Uri.EscapeDataString(callbackUrl),
Uri.EscapeDataString(this.ConsumerKey),
Uri.EscapeDataString(oauthNonce),
Uri.EscapeDataString(oauthSignatureMethod),
Uri.EscapeDataString(oauthTimestamp),
Uri.EscapeDataString(oauthVersion));
var baseString = string.Format("POST&{0}&{1}", Uri.EscapeDataString(oauthUrl.AbsoluteUri), Uri.EscapeDataString(authSignature));
var compositeKey = string.Concat(Uri.EscapeDataString(this.ConsumerSecret), "&");
using (HMACSHA1 hasher = new HMACSHA1(Encoding.ASCII.GetBytes(compositeKey)))
{
oauthSignature = Convert.ToBase64String(hasher.ComputeHash(Encoding.ASCII.GetBytes(baseString)));
}
var authHeader = string.Format("oauth oauth_callback=\"{0}\", oauth_consumer_key=\"{1}\", oauth_nonce=\"{2}\", oauth_signature=\"{3}\", oauth_signature_method=\"{4}\", oauth_timestamp=\"{5}\", oauth_version=\"{6}\"",
Uri.EscapeDataString(callbackUrl),
Uri.EscapeDataString(this.ConsumerKey),
Uri.EscapeDataString(oauthNonce),
Uri.EscapeDataString(oauthSignature),
Uri.EscapeDataString(oauthSignatureMethod),
Uri.EscapeDataString(oauthTimestamp),
Uri.EscapeDataString(oauthVersion));
ServicePointManager.Expect100Continue = false;
HttpWebRequest authRequest = (HttpWebRequest)System.Net.WebRequest.Create(oauthUrl);
authRequest.Method = "POST";
authRequest.Headers.Add("Authorization", authHeader);
authRequest.Headers.Add("Accept-Encoding", "gzip");
authRequest.ContentType = "application/x-www-form-urlencoded;charset=UTF-8";
authRequest.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
try
{
using (var response = authRequest.GetResponse() as HttpWebResponse)
{
if (response.StatusCode != HttpStatusCode.OK)
{
throw new Exception(String.Format("Server error (HTTP {0}: {1}).", response.StatusCode, response.StatusDescription));
}
var stream = response.GetResponseStream();
var sr = new StreamReader(stream);
return sr.ReadToEnd();
}
}
catch (WebException ex)
{
using (var stream = ex.Response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
I always get a 401 error back.
While in the application set-up at https://apps.twitter.com/ a callback URL is required, the OAuth 1.0A spec mandates that a callback URL be passed with the request token. You can pass any valid URL to with that request, effectively having each of your callbacks go to the unique URL for each of your users. So, you're not locked into the callback URL in your application's set-up.
Related
I have a web app running in Azure which has azure Active Directory authentication enabled. This is given below (I have configured this correctly there is no issue with this): -
Now I want to call one of the API of this web app. Code for getting access token based on the client credentials: -
public static string GetAccessToken()
{
string authContextURL = "https://login.microsoftonline.com/" + "TENANT_ID";
var authenticationContext = new AuthenticationContext(authContextURL);
var credential = new ClientCredential("CLIENT_ID", "CLIENT_SECRET");
var result = authenticationContext.AcquireTokenAsync("URL_FOR_MY_WEBAPP", credential).Result;
if (result == null)
{
throw new InvalidOperationException("Failed to obtain the token");
}
string token = result.AccessToken;
return token;
}
Code for calling the desired API: -
private static string GET(string URI, string token)
{
Uri uri = new Uri(string.Format(URI));
// Create the request
var httpWebRequest = (HttpWebRequest)WebRequest.Create(uri);
httpWebRequest.Headers.Add(HttpRequestHeader.Authorization, "Bearer " + token);
httpWebRequest.ContentType = "application/json";
httpWebRequest.Method = "GET";
// Get the response
HttpWebResponse httpResponse;
try
{
httpResponse = (HttpWebResponse)httpWebRequest.GetResponse();
}
catch (Exception ex)
{
return ex.Message;
}
string result = null;
using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
result = streamReader.ReadToEnd();
}
return result;
}
I am getting an unauthorized error while getting the response. Could anyone tell what is wrong here? The same service principal is working with graph client. Any help or suggestion will be appreciated.
The resource to acquire access token is not correct. You should use the same client id of your AD app.
var result = authenticationContext.AcquireTokenAsync("{CLIENT_ID}", credential).Result;
I'm doing a simple page to upload document. However after I get the input I need to call a web API to get a token which will then allow me to call another web API which will finally proceed to upload the file (using the file AND the token). So what I want to know is how to call this API and retrieve it's response and then add that response (which would be the token).
I am trying following code it works fine in console application but not work on web Application.
here is my code
public void GetTokenpost()
{
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
string url = "mydomain/api/oauth/token";
HttpWebRequest request = (HttpWebRequest)HttpWebRequest.Create(url);
request.Method = "POST";
string data = "grant_type=urn:ietf:params:oauth:grant-type:jwt-bearer&assertion=here jwt token"; // make sure this is URL encoded
request.ContentType = "application/json";
using (var streamWriter = new StreamWriter(request.GetRequestStream()))
{
streamWriter.Write(data);
streamWriter.Flush();
streamWriter.Close();
var HttpResponse = (HttpWebResponse)request.GetResponse();
using (var streamRead = new StreamReader(HttpResponse.GetResponseStream()))
{
var value1 = streamRead.ReadToEnd();
Console.WriteLine("Hello {0}",
}
}
}
using (var client = new HttpClient())
{
client.BaseAddress = new Uri("http://domain");
//Called Member default GET All records
//GetAsync to send a GET request
// PutAsync to send a PUT request
var responseTask = client.GetAsync(string requestUri);
responseTask.Wait();
//To store result of web api response.
var result = responseTask.Result;
//If success received
if (result.IsSuccessStatusCode)
{
//your code for performing action
}
I want to redirect a system to a centralized authentication server and I need to fill some parameters in headers and redirect to the authentication server completely. Using Web Client or Web Request I must return a value as a response to the requester ( They work as a listener ).
WebClient Send Request Example:
var values = new NameValueCollection();
values["clientId"] = clientId;
values["clientIP"] = currentIP;
byte[] response;
var resultResponse = string.Empty;
using (var client = new WebClient())
{
try {
response = client.UploadValues(url, values);
resultResponse = Encoding.Default.GetString(response);
}
catch (WebException e)
{
string exception = string.Empty;
if (e.Status == WebExceptionStatus.ProtocolError)
{
exception += ((HttpWebResponse)e.Response).StatusCode;
exception += ((HttpWebResponse)e.Response).StatusDescription;
}
}
}
Web Request Example
string method = "post";
WebRequest req = WebRequest.Create(uri);
req.ContentType = contentType;
req.Method = method;
req.Headers.Add("myhead", value);
req.ContentLength = jsonDataBytes.Length;
var stream = req.GetRequestStream();
stream.Write(jsonDataBytes, 0, jsonDataBytes.Length);
stream.Close();
var response = req.GetResponse().GetResponseStream();
StreamReader reader = new StreamReader(response);
var respo = reader.ReadToEnd();
reader.Close();
response.Close();
return respo;
As you see in both methods the requester post the request and is waiting till receiving the response. I can not return to the requester system until I show two view to the user, get the username and passwords and finally process the information. I need to fill headers in a request, post it to the server and also redirect to the central authentication server. I searched a lot and found that it is impossible to post and redirect at a same time. Can you suggest any other methods to me?
Just started at a new company and we all use Jira, the customers are determined to not use it as they don't like it so I have decided to build a simple Windows Form when they can both Log tickets and get Updates and Comments in a nice simple UI.
Now I have never done any coding before 2 weeks ago so it has been a struggle to get my head around both C# and Rest (Have made scripts for basic IT fixes but never anything as complex as this!)
Back onto point, Set up and got a Rest API set up with a Rest Client but everytime I try pull data from a ticket on Jira I get the error:
{"errorMessages":["You do not have the permission to see the specified issue.","Login Required"],"errors":{}}
Here is the code from the Form:
private void button3_Click_1(object sender, EventArgs e)
{
var client = new RestClient("https://jira.eandl.co.uk/rest/api/2/issue/ITREQ-" + textBox1.Text
);
client.Authenticator = new SimpleAuthenticator("username", "abc", "password", "123");
var request = new RestRequest(Method.GET);
request.AddParameter("token", "saga001", ParameterType.UrlSegment);
// request.AddUrlSegment("token", "saga001");
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var queryResult = client.Execute(request);
Console.WriteLine(queryResult.Content);
}
And here is the code from the Rest Client itself:
public Restclient()
{
endPoint = string.Empty;
httpMethod = httpVerb.GET;
}
private string logonAttempt;
public string makeRequest()
{
string strResponseValue = string.Empty;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(endPoint);
request.Method = httpMethod.ToString();
String authHeader = System.Convert.ToBase64String(System.Text.ASCIIEncoding.ASCII.GetBytes(userName + ":" + userPassword));
request.Headers.Add("Authorization", authType.ToString() + " " + authHeader);
HttpWebResponse response = null;
try
{
response = (HttpWebResponse)request.GetResponse();
//Process the Response Stream... (Could be JSON, XML ot HTML etc...)
using (Stream responseStream = response.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
strResponseValue = reader.ReadToEnd();
}//End of Stream Reader
}
}//end of Response Stream
}
catch (Exception ex)
{
strResponseValue = "(\"errorMessages\":[\"" + ex.Message.ToString() + "\"],\"errors\":{}}";
}
finally
{
if(response != null)
{
((IDisposable)response).Dispose();
}
}
return strResponseValue;
}
}
}
Now obviously I am expecting that I have missed something absolutely bigginer as like I said, I've never taken on a project like this before and had 0 experience.
Just looking for someone to bluntly tell me what I'm doing wrong
Changed to this as per answer:
private void button3_Click_1(object sender, EventArgs e)
{
var client = new
RestClient("https://jira.eandl.co.uk/rest/api/2/issue/ITREQ-" + textBox1.Text
);
client.Authenticator = new HttpBasicAuthenticator("username", "password");
var request = new RestRequest(Method.GET);
string authHeader = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes("cdale!" + ":" + "Chantelle95!"));
request.AddHeader("Authorization", "Basic " + authHeader);
request.OnBeforeDeserialization = resp => { resp.ContentType = "application/json"; };
var queryResult = client.Execute(request);
Console.WriteLine(queryResult.Content);
}
By default with the Jira REST API, you can use Basic Authentication or OAuth2. I think that more easy way for you will be to use the Basic one.
I'm not sure why you have a class where you define your custom RestClient since the first block of code uses the RestSharp one from http://restsharp.org.
In this case, you will need to modify your authenticator:
client.Authenticator = new HttpBasicAuthenticator(userName, password);
And I think that you should remove the line where you specify a token. I don't think that it's required.
Finally, the class Restclient doesn't seem to be used, then remove it.
You could also uses what you have created in your custom RestClient and manually specify a Basic header:
string authHeader = Convert.ToBase64String(ASCIIEncoding.ASCII.GetBytes(userName + ":" + userPassword));
request.AddHeader("Authorization", "Basic " + authHeader);
However, it's essentially the behavior of the HttpBasicAuthenticator class.
If you don't want to encode your credentials in every request here is how to do it using cookies.
When requesting the cookie you don't need to add any authorization on the headers. This method will accept a JSON string with the user name and password and the URL. It will return the cookie values.
public async Task<JiraCookie> GetCookieAsync(string myJsonUserNamePassword, string JiraCookieEndpointUrl)
{
using (var client = new HttpClient())
{
var response = await client.PostAsync(
JiraCookieEndpointUrl,
new StringContent(myJsonUserNamePassword, Encoding.UTF8, "application/json"));
var json = response.Content.ReadAsStringAsync().Result;
var jiraCookie= JsonConvert.DeserializeObject<JiraCookie>(json);
return jArr;
}
}
public class JiraCookie
{
public Session session { get; set; }
}
public class Session
{
public string name { get; set; }
public string value { get; set; }
}
When I call it using url: http://[baseJiraUrl]/rest/auth/1/session it returns the following JSON response:
{
"session" : -{
"name" : JSESSIONID,
"value" : cookieValue
}
Keep in mind the URL above is valid in the version of JIRA I'm using and may vary depending on which version you're using. Read the JIRA API documentation for the correct URL for the version you are using. I'm using the following:
https://docs.atlassian.com/software/jira/docs/api/REST/7.6.1/#auth/1/session
Remember you'll have to store your cookie and use it on every subsequent request.
Check out this answer on how add cookies to your HttpClient request: How do I set a cookie on HttpClient's HttpRequestMessage.
Once you're done with the cookie (logging out) simply send a delete http request with the same URL as the post.
Reference: https://stackoverflow.com/a/49109192/7763903
I found that this code works as expected:
var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));
The following code however gives me a headache:
var url = "https://limal.info/efulfilment.php";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var request = (HttpWebRequest)WebRequest.Create(url);
request.ContentType = "application/x-www-form-urlencoded";
request.Method = "POST";
request.Timeout = 5000; // I added this for you, so you only need to wait 5 sec...
using (var requestStream = request.GetRequestStream())
{
var writer = new StreamWriter(requestStream);
writer.Write("xml=test");
}
using (var response = request.GetResponse())
{
using (var responseStream = response.GetResponseStream())
{
var reader = new StreamReader(responseStream);
var answer = reader.ReadToEnd();
}
}
Somehow the post parameters don't get recognized and I get the response:
"limal.info bridge error: Missing 'xml' variable in post request."
(The correct answer would be that the XML data is wrongly formatted, as test is invalid XML...)
Now to the next Problem:
When I use a different url a timeout exception occurs. It hangs at UploadValues in the following code. (The other example that uses HttpWebRequest hangs at GetResponse, which I tried, too.)
var url = "https://sys.efulfilment.de/rt/";
ServicePointManager.ServerCertificateValidationCallback = delegate { return true; };
var alternativeAnswer = Encoding.UTF8.GetString(new WebClient().UploadValues(url, new NameValueCollection() { { "xml", "test" } }));
I read about similar problems here and on other sites. It seems that using Http POST with SSL is a problem in .NET.
WHY?? :(