How can one authenticate to the bing search api without using the BingSearchContainer.
With bing search container we can do something like this:
var bing = new BingSearchContainer(new Uri("https://api.datamarket.azure.com/Bing/Search/")) { Credentials = new NetworkCredential(bingKey, bingKey) };
But this doesn't help me because i need to pass some other proxy to access the internet.
Can anyone help me with this?
One way is to create a WebRequest with basic authorization.
Like this:
//some demo uri
Uri uri = new Uri("https://api.datamarket.azure.com/Bing/Search/Composite?Sources=%27web%27&Query=%27xbox%27&$format=json");
System.Net.WebRequest req = System.Net.WebRequest.Create(uri);
byte[] byteKey = System.Text.ASCIIEncoding.ASCII.GetBytes(bingKey + ":" + bingKey);
string stringKey = System.Convert.ToBase64String(byteKey);
req.Headers.Add("Authorization", "Basic " + stringKey);
req.Proxy = new System.Net.WebProxy("proxy_url", true);
req.Proxy.Credentials = new NetworkCredential("", "", "");
System.Net.WebResponse resp = req.GetResponse();
Related
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();
}
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.
I've been struggling with authentication in TeamCity through the API lately. I can access the resources directly in my browser (http://usr:pw#teamcity:8111/httpAuth/app/rest/...), but doing so programmatically returns 401-Unauthorized.
WebRequest request = WebRequest.Create("http://user:pwd#teamcity:8111/httpAuth/app/rest/projects");
request.Method = WebRequestMethods.Http.Get;
try
{
request.Timeout = Timeout.Infinite;
WebResponse response = request.GetResponse(); //Returns 401:Unauthorized
I can use guestAuth(http://teamcity:8111/guestAuth/app/rest/projects) without any problem, so there should not be any problem with the WebRequest itself.
Does anyone have an idea?
Try to add your credentials and then make request.it will be get what you need.
var username = "abc";
var password = "123";
var encoded = System.Convert.ToBase64String(System.Text.Encoding.GetEncoding("ISO-8859-1").GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
I'm trying to get this bit of code to pause/unpause my XBMC Player
HttpWebRequest request = (HttpWebRequest)WebRequest.Create(baseUrl+playPause);
HttpWebResponse response = (HttpWebResponse)request.GetResponse();
Stream resStream = response.GetResponseStream();
Debug.WriteLine(resStream);
And I need to send my login data with it, lets say its MyUsername and SuperPassword.
How would I add that data intp the request? I tried request.Headers.Add("Authorization", "MyUsername SuperPassword"); but I still get the 401 error from it.
EDIT: Nevermind, i figured it out
String username = "MyUsername";
String password = "SuperPassword";
String encoded = System.Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(username + ":" + password));
request.Headers.Add("Authorization", "Basic " + encoded);
There is a awesome post about Send login details with HttpWebRequest and the link is post link
if You feel brother then you can also try one my Best solve this by using webClient class. example:
var url = #"...";
var mybar= new System.Collections.Specialized.NameValueCollection();
mybar.Add("username", "username");
mybar.Add("password", "password");
var client = new System.Net.WebClient();
var data = client.UploadValues(url, mybar);
var res = System.Text.Encoding.ASCII.GetString(data);
Console.WriteLine(res);
Console.ReadLine();
I am using the following code on Page_Load of facebook_login page in asp.net web application. After redirecting this url I have got the accesstoken in url but when I try to access this current url via HttpContext.Current.Request.Url.AbsoluteUri it gave the url of app where it is published not the url which is currently in window.
How can I access this access token or userdetails?
var uriParams = new Dictionary<string, string>() {
{"client_id", facebookAppId},
{"response_type", "token"},
{"scope", "user_about_me, read_stream, email"},
{"redirect_uri", "http://apps.facebook.com/appNameHere/"}
};
StringBuilder urlBuilder = new StringBuilder();
foreach (var current in uriParams)
{
if (urlBuilder.Length > 0)
{
urlBuilder.Append("&");
}
var encoded = HttpUtility.UrlEncode(current.Value);
urlBuilder.AppendFormat("{0}={1}", current.Key, encoded);
}
var loginUrl = "http://www.facebook.com/dialog/oauth?" + urlBuilder.ToString();
Response.Redirect(loginUrl);
In Facebook you can set call back url value which should point to your web application. So once you make request to http://www.facebook.com/dialog/oauth and after successfully login Facebook redirect to Callback Url (which is your web application).
Then you have to make call to url : https://graph.facebook.com/oauth/access_token something like this:
StringBuilder uri = new StringBuilder();
uri.Append("https://graph.facebook.com/oauth/access_token?");
uri.Append("client_id=" + ClientKey + "&");
uri.Append("redirect_uri=" + Curl + "&");
uri.Append("scope=offline_access&");
uri.Append("client_secret=" + ClientSecret + "&");
uri.Append("code=" + OAuthCode);
HttpWebRequest req = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(uri);
req.Headers.Add("Authorization", String.Empty);
req.Method = "POST";
req.ServicePoint.Expect100Continue = false;
req.ContentLength = 0;
req.ContentType = "application/x-www-form-urlencoded";
HttpWebResponse resp;
try
{
resp = (HttpWebResponse)req.GetResponse();
System.IO.StreamReader sr = new System.IO.StreamReader(resp.GetResponseStream());
string result = sr.ReadToEnd().Trim();
string[] resultCollection = Regex.Split(result, "&");
string access_token = Regex.Split(resultCollection[0], "=")[1];//This is the access token
//which you want and you can save it to database or some where for further use.
}
catch (WebException ex)
{
resp = (HttpWebResponse)ex.Response;
//pass on the exception
throw ex;
}
UPDATE :
OAuthCode you can get like this, it is available when after successfully login FB redirect to your web app page
OAuthCode = Request.Params["code"];
Header you can pass empty like String.Empty