I'm using Xero with c# winforms. Xero released a new topic OAuth2.0 and they are providing sample only for web applications. But I didn't get authorization code. I'm getting an html page as response not the authorization code. Can anyone help me? Thanks for the help in advance.
Here is my current code for getting authorization code:
string XeroUrl = "login.xero.com/identity/connect/authorize?response_type=code&client_id=xxxxxxxxxxxx&redirect_uri=xxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
string ScopeLimit = "openid profile email accounting.transactions&state=123";
string URL = "https://" + XeroUrl + "&scope=" + ScopeLimit;
try {
HttpWebRequest webRequest = (HttpWebRequest)WebRequest.Create(URL);
webRequest.AllowAutoRedirect = true;
webRequest.Method = WebRequestMethods.Http.Get;
webRequest.UseDefaultCredentials = false;
webRequest.PreAuthenticate = false;
HttpWebResponse response = null;
response = (HttpWebResponse)webRequest.GetResponse();
StreamReader reader = new StreamReader(response.GetResponseStream());
string output = reader.ReadToEnd();
}
Related
I'm trying to download file contents from Bit Bucket, but I keep getting a "Log In" page as a response. I'm providing credentials with Basic Auth. Is this a C# specific issue? Everything works fine if I try it via Postman. Code Below.
var url = "https://[BITBUCKET_DOMAIN]/projects/[ID]/repos/[REPO]/raw/[PATH_TO_MY_FILE]"
var uri = new Uri(url);
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(uri);
string userName = "user";
string password = "pw";
request.PreAuthenticate = true;
request.Credentials = new NetworkCredential(userName, password);
request.Accept = "text/plain";
string result;
using (var response = request.GetResponse())
using (var stream = response.GetResponseStream())
using (var reader = new StreamReader(stream))
{
result = reader.ReadToEnd();
}
return result;
This top answer here worked for me.
HttpWebRequest using Basic authentication
String encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(userName + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
I am using json auth api to authenticate the user.
i am using my wordpress site hosted on 000webhost with https protocol.
When i am logging in i am getting this error message :
System.Net.WebException: The remote server returned an error: (401) Unauthorized
What I have tried:
var webRequest =(HttpWebRequest)WebRequest.Create("https://mahdii.000webhostapp.com/users/auth/generate_auth_cookie/?nonce=my_nonce&username=my_username&password=my_password");
webRequest.Method = "GET";
webRequest.ContentLength = 0; // added per comment
string autorization = "username" + ":" + "Password";
byte[] binaryAuthorization =System.Text.Encoding.UTF8.GetBytes(autorization);
autorization = Convert.ToBase64String(binaryAuthorization);
autorization = "Basic " + autorization;
webRequest.Headers.Add("AUTHORIZATION", autorization);
var webResponse = (HttpWebResponse)webRequest.GetResponse();
if (webResponse.StatusCode != HttpStatusCode.OK) Console.WriteLine("{0}", webResponse.Headers);
using (StreamReader reader = new StreamReader(webResponse.GetResponseStream()))
{
string s = reader.ReadToEnd();
Console.WriteLine(s);
reader.Close();
}
Please i need help,suggestions would be appreciated.
I am pretty sure all the url parameters are correct !
I'm trying to log in to a server (REST API) which uses HTTP Basic Authentication. The request looks like this:
public JObject PerformLogin(string username, string password)
{
string html = string.Empty;
this.username = username;
this.password = password;
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(auth_url_internal);
request.AllowAutoRedirect = true;
request.AutomaticDecompression = DecompressionMethods.GZip | DecompressionMethods.Deflate;
request.Method = "GET";
request.CookieContainer = cookies;
request.KeepAlive = true;
//request.ServicePoint.Expect100Continue = false;
request.Accept = "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8";
request.Headers.Add("Accept-Language", "de-DE,de;q=0.8,en-US;q=0.6,en;q=0.4");
request.PreAuthenticate = true;
request.AuthenticationLevel = System.Net.Security.AuthenticationLevel.MutualAuthRequested;
string authInfo = username + ":" + password;
authInfo = Convert.ToBase64String(System.Text.Encoding.Default.GetBytes(authInfo));
request.Headers.Add("Authorization", "Basic " + authInfo);
using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
using (Stream stream = response.GetResponseStream())
using (StreamReader reader = new StreamReader(stream))
{
html = reader.ReadToEnd();
}
JObject jresponse = JObject.Parse(html);
sess_url_internal = jresponse["internalUrl"].ToString();
sess_url_public = jresponse["publicUrl"].ToString();
return jresponse;
}
which basically works, however the credentials are being sent too early.
First I used curl to see what the traffic looks like in detail and found a "Location:"-Header, which means that there is a redirect happening. In detail, the server redirects me from /api/rest/authenticate?version=1.0, which is the authentication URL (lets call it URL1), to /authenticationbasic/login?AlcApplicationUrl=/api/rest/authenticate%3fversion=1.0 (URL2).
However, Chrome sends the Credentials to URL2, where my program sends them to URL1 which is too early, because the server expects them at URL2, where my application doesn't send any and therefore gets a false return.
How can i change that behaviour?
So with the kind help of x... I figured out how to do it:
After the HttpWebResponse response = (HttpWebResponse)request.GetResponse(); simply add
if ((int)response.StatusCode == 302) // redirect
{
/*
Call the function recursively with the new URL, found in
response.Headers["Location"], in my case this would be:
*/
auth_url_internal = response.Headers["Location"];
return PerformLogin(username, password);
}
Here is the code that I am using:
public static void FetchXML()
{
_url = new Uri("http://api.tumblr.com/v2/blog/" + _username + ".tumblr.com/likes?api_key=REc3Z6l4ZYss11a8lX6KKje0X8Hsi9U77SyaPbQrOBBCGJGA6D");
var client = new RestClient();
client.Authority = _url.ToString();
var request = new RestRequest();
request.AddParameter("limit", "20");
request.AddParameter("offset", _offset.ToString());
var response = client.Request(request);
var content = response.Content.ToString();
var parsedResponse = JsonParser.FromJson(content);
}
If I take the Uri value and paste it into my browser (using a valid Tumblr username) I'm getting the correct Json, but in my application the content of response is:
"{\"meta\":{\"status\":401,\"msg\":\"Unauthorized\"},\"response\":[]}"
Anyone have any idea why this is? According to the Tumblr API
retrieving likes should only need the API key, which I am providing.
Hi you can use the below code to get the response.
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(URL);
request.Method = "GET";
request.ContentType = "Application/json";
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream receive = response.GetResponseStream();
StreamReader reader = new StreamReader(receive, Encoding.UTF8);
string respond = reader.ReadToEnd();
With reference from this URI:
http://developer.okta.com/docs/api/resources/oidc.html#parameter-details
I have tried using the /authorize endpoint but I am not getting a token back in the result from Okta, I always get the result as:
404 - Page Not Found
The page you are looking for can't be found. Please go to your applications page and try again.
Can anyone help me on this? Sent email to Okta developer but I'm not getting a reply.
var url = "https://xxx-admin.okta.com/oauth2/v1/authorize";
//string urlParams =
// string.Format("response_type={0}&client_id={1}&redirect_uri={2}&scope={3},state={4},nonce={5}", "code",
// "xxxxxx", redirecturl, "email", "email", );
var urlParameters =
$"?idp={"okta"}&response_type={"id_token"}&client_id={"xxxxx"}&redirect_uri={redirecturl}" +
$"&scope={"openid profile email address"}&response_mode={"form_post"}&state={"email"}&nonce={"nonce"}";
string redirecturl = "http://localhost/";
//grant_type,code,refresh_token,scope,redirect_uri,client_id,client_secret
var urlParameters = $"?grant_type={"password"}&code={""}&refresh_token={""}&redirect_uri={redirecturl}&" +
$"client_id=xxxxx&client_secret=yyyyyy";
var url = "https://xxxx-admin.okta.com/oauth2/v1/token";
HttpWebRequest req = (HttpWebRequest)WebRequest.Create(url+ urlParameters);
req.Method = "POST";
req.ContentType = "application/x-www-form-urlencoded";
var strRequest= string.Empty;
StreamWriter streamOut = new StreamWriter(req.GetRequestStream(), System.Text.Encoding.ASCII);
StreamReader streamIn = new StreamReader(req.GetResponse().GetResponseStream());
string strResponse = streamIn.ReadToEnd();
streamIn.Close();
return strResponse.ToString();
Looks like you're doing a POST for your method. I think it should be a GET.