SquareUp Request token: Forbidden - c#

I'm working in .NET 4.6.2 and had binding issues when trying to reference the Square.Connect library which is Standard 2.0 so I'm trying to manually code things.
public ActionResult RequestToken(string code)
{
if (!string.IsNullOrEmpty(code))
{
string baseUrl = Request.Url.Scheme + "://" + Request.Url.Authority + Request.ApplicationPath.TrimEnd('/') + "/";
SquareUpRequest squareUpRequest = new SquareUpRequest()
{
client_id = "Application ID (Not sandbox)",
client_secret = "Personal Access Token",
code = code
};
var client = new RestSharp.RestClient(baseUrl);
var request = new RestSharp.RestRequest("https://connect.squareup.com/oauth/token", RestSharp.Method.POST);
request.RequestFormat = RestSharp.DataFormat.Json;
request.AddBody(squareUpRequest);
request.AddHeader("Authorization", "Client OAuthApplicationSecret");
var response = client.Execute(request);
}
return View();
}
I'm assuming that I can request a token many times. SquareUpRequest is a custom class.
Do you seen anything incorrect?

Wow. I just noticed after a few hours of hell that:
var request = new RestSharp.RestRequest("https://connect.squareup.com/oauth/token", RestSharp.Method.POST);
should be
var request = new RestSharp.RestRequest("https://connect.squareup.com/oauth2/token", RestSharp.Method.POST);

Related

C# Consuming web service wsdl with access token

I'm trying to consume a WSDL web service and it requires an access token to be sent in the header. However, I keep getting a 401 error and I'm not sure if I am injecting the token correctly.
Heres a snippet of the code:
var client = new WsldClient();
var operationContext = new OperationContext(client.InnerChannel);
using (new OperationContextScope(operationContext))
{
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Bearer " + accessToken
operationContext.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
client.SomeMethod();
}
This returns a 401 error.
You can try the following code:
var client = new MyClient();
client.ClientCredentials.UserName.UserName = "username";
client.ClientCredentials.UserName.Password = "password";
var httpRequestProperty = new HttpRequestMessageProperty();
httpRequestProperty.Headers[HttpRequestHeader.Authorization] = "Basic " + Convert.ToBase64String(Encoding.ASCII.GetBytes(client.ClientCredentials.UserName.UserName + ":" + client.ClientCredentials.UserName.Password));
var context = new OperationContext(ormClient.InnerChannel);
using (new OperationContextScope(context))
{
context.OutgoingMessageProperties[HttpRequestMessageProperty.Name] = httpRequestProperty;
return await client.SomeMethod();
}
Authorization Header is missing in Http request using WCF
http://plainoldstan.blogspot.com/2008/07/avoid-http-401-roundtrip-with-adding.html

External Login using Custom API

I have 2 projects one Web API that have a simple /Token api that returns a token for the logged in user
and the second project is .NET Core that will use the URL/Token method in the login form.
Here is the code on the method that is used to login in the second project
public async Task<IActionResult> OnPostAsync(string returnUrl = null)
{
var requestBody = new { grant_type = "password", username = Input.Email, password = Input.Password };
var data = Newtonsoft.Json.JsonConvert.SerializeObject(requestBody);
using (var client = new HttpClient())
using (var req = new HttpRequestMessage(HttpMethod.Get, "http://localhost:49470/Token"))
{
req.Content = new StringContent("{\"grant_type\":\"password\",\"username\":" + Input.Email + ",\"password\":" + Input.Password + "}", Encoding.UTF8, "application/json");
using (var rep = await client.SendAsync(req))
{
rep.EnsureSuccessStatusCode();
var content = await rep.Content.ReadAsStringAsync();
}
}
}
}
Error i'm facing:
The problem that i'm facing i always get a 400 (Bad Request) Error, ps: i'm new in .netcore
I've solved it, the problem was the api\Token body didn't accept the structure of the json parsed string
Simply i've used
var response = client.PostAsync("http://localhost:49470/Token", new StringContent("grant_type=password&username=" + Input.Email + "&password=" + Input.Password, Encoding.UTF8, "application/json")).Result;

Shopify access Token returning null

Near the end of my journey I realized that my access Token was coming up null. Im using RectSharp in visual studio with C# to get a shopify app up and running, everything works up to pressing install, where then I get brought back to visual studio and told that the accessToken is null D:
Here is some pictures of the errors and my debuging
click here for image
this is the error I noticed poping up around r, which is written in rectSharp, and looks correct to me.
{{
"error": "822: unexpected token at 'client_id=6e83a9e8b79dccbad77530c7273e3e00&client_secret=03cfa0ef9b451868bb0160e4ad75e0ec&code=3a941fe03d9fc0ea0d4226f9f4af316f'"
}}
here is my auth code:
public ActionResult install(string shop, string signature, string timestamp)
{
string r = string.Format("https://{0}/admin/oauth/authorize?client_id={1}&scope=read_fulfillments,write_fulfillments,read_orders,write_products&redirect_uri=https://{2}/fulfillment/auth", shop, apiKey, appUrl);
return Redirect(r);
}
public ActionResult auth(string shop, string code)
{
string u = string.Format("https://{0}/admin/oauth/access_token", shop);
var client = new RestClient(u);
var request = new RestRequest(Method.POST);
request.RequestFormat = DataFormat.Json;
request.AddHeader("Content-Type", "application/json");
request.AddParameter("application/x-www-form-urlencoded", "client_id=" + apiKey + "&client_secret=" + secretKey + "&code=" + code, ParameterType.RequestBody);
var response = client.Execute(request);
var r = JsonConvert.DeserializeObject<dynamic>(response.Content);
var accessToken = r.access_token;
accesstokenmain = r.access_token;
//save shop token and information to database
SaveShopToken(shop, (string)accessToken);
//Part 5
//create a un-install web hook
//you want to know when customers delete your app from their shop
string unInstallUrlCallback = "https://8a047f39.ngrok.io/fulfillment/uninstall";
string shopAdmin = string.Format("https://{0}/admin/", shop);
var webHook = new WebHookBucket();
webHook.Whook = new WebHook { Format = "json", Topic = "app/uninstalled", Address = unInstallUrlCallback };
CreateUninstallHook(shopAdmin, "webhooks.json", Method.POST, (string)accessToken, webHook);
return Redirect(AskCustomerCharge(shop, (string)accessToken));
// return View();
}
public void SaveShopToken(string shop, string token)
{
using (var con = new fulfillmentdbEntities())
{
if (con.ShopTokens.Any(c=>c.Shop == shop))
{
} else
{
con.ShopTokens.Add(new ShopToken { Shop = shop, Token = token, InstallDate = DateTime.Now });
}
con.SaveChanges();
}
}
Please try by changing the request.AddHeader and request.AddParameter as below
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", $"client_id={apiKey}&client_secret={secretKey}&code={code}, ParameterType.RequestBody);

Not getting access_token by calling https://login.microsoftonline.com/{tenant}/oauth2/token

I want to get user email from user id (object identifier) from web api, but getting blank response while calling api for token. I am running this code from my Web API. Please help. Below is the code.
Given full permission to APIs
Getting Blank response in below line.
var responseBytes = await webClient.UploadValuesTaskAsync(url, "POST", requestParameters);
Below is code
var tenant = "tenant ID";
var clientID = "app ID";
// I've tried graph.microsoft.com and graph.microsoft.com/.default
var resource = "https://graph.microsoft.com";
var secret = "client secret";
string token;
using (var webClient = new WebClient())
{
var requestParameters = new NameValueCollection();
requestParameters.Add("scope", resource);
requestParameters.Add("client_id", clientID);
requestParameters.Add("grant_type", "client_credentials");
requestParameters.Add("client_secret", secret);
var url = "https://login.microsoftonline.com/{tenant}/oauth2/token";
webClient.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
var responseBytes = await webClient.UploadValuesTaskAsync(url, "POST", requestParameters);
var responseBody = Encoding.UTF8.GetString(responseBytes);
var jsonObject = Newtonsoft.Json.JsonConvert.DeserializeObject<Newtonsoft.Json.Linq.JObject>(responseBody);
token = jsonObject.Value<string>("access_token");
}
HttpClient client = new HttpClient();
client.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
var response = await client.GetAsync(new Uri("https://graph.microsoft.com/v1.0/user/" + ClaimsPrincipal.Current.FindFirst("http://schemas.microsoft.com/identity/claims/objectidentifier")));
Your error is here:
requestParameters.Add("scope", resource);
It needs to be resource rather than scope:
requestParameters.Add("resource", resource);
Can you help me understand what documentation or tutorial you followed to make this mistake? I have seen it happen before and I am trying to understand the patterns here.
The documentation and authentication flow you should be following is here.

RestSharp post request - Body with x-www-form-urlencoded values

I am using postman and making an api post request where I am adding body with x-www-form-urlencoded key/values and it works fine in postman.
The issue arrises when I try it from c# using RestSharp package.
I have tried the following code below but not getting the response. I get "BadRequest" invalid_client error.
public class ClientConfig {
public string client_id { get; set; } = "value here";
public string grant_type { get; set; } = "value here";
public string client_secret { get; set; } = "value here";
public string scope { get; set; } = "value here";
public string response_type { get; set; } = "value here";
}
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
req.AddHeader("Content-Type","application/x-www-form-urlencoded");
req.AddParameter("application/x-www-form-urlencoded",config,ParameterType.RequestBody);
var res = client.Execute(req);
return;
}
//Also tried this
req.AddParameter("client_id",config.client_id,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("grant_type",config.grant_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("client_secret",config.client_secret,"application/x-www-form-urlencoded",ParameterType.RequestBody);
req.AddParameter("scope",config.scope,ParameterType.RequestBody);
req.AddParameter("response_type",config.response_type,"application/x-www-form-urlencoded",ParameterType.RequestBody);
//tried this too
var client = new RestClient("url-here");
var req = new RestRequest("endpointhere",Method.POST);
var config = new ClientConfig();
req.AddBody(config);
var res = client.Execute(req);
this working for me, it was generator from postman
var token = new TokenValidation()
{
app_id = CloudConfigurationManager.GetSetting("appId"),
secret = CloudConfigurationManager.GetSetting("secret"),
grant_type = CloudConfigurationManager.GetSetting("grant_type"),
Username = CloudConfigurationManager.GetSetting("Username"),
Password = CloudConfigurationManager.GetSetting("Password"),
};
var client = new RestClient($"{xxx}{tokenEndPoint}");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", $"app_id={token.app_id}&secret={token.secret}&grant_type={token.grant_type}&Username={token.Username}&Password={token.Password}", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
if (response.StatusCode != HttpStatusCode.OK)
{
Console.WriteLine("Access Token cannot obtain, process terminate");
return null;
}
var tokenResponse = JsonConvert.DeserializeObject<TokenValidationResponse>(response.Content);
I personally find this way to work better for me when sending Form-UrlEncoded data.
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
// Content type is not required when adding parameters this way
// This will also automatically UrlEncode the values
req.AddParameter("client_id",config.client_id, ParameterType.GetOrPost);
req.AddParameter("grant_type",config.grant_type, ParameterType.GetOrPost);
req.AddParameter("client_secret",config.client_secret, ParameterType.GetOrPost);
req.AddParameter("scope",config.scope, ParameterType.GetOrPost);
req.AddParameter("response_type",config.response_type, ParameterType.GetOrPost);
var res = client.Execute(req);
return;
}
Details on this parameter type can be found here:
https://github.com/restsharp/RestSharp/wiki/ParameterTypes-for-RestRequest#getorpost
Personally, I found AddObject() method quite useful, and cleaner when you have so many parameters to add.
public void GetResponse() {
var client = new RestClient("api-url-here");
var req = new RestRequest("endpoint-here",Method.POST);
var config = new ClientConfig();//values to pass in request
req.AddHeader("Content-Type","application/x-www-form-urlencoded");
req.AddObject(config);
var res = client.Execute(req);
return res;
}
If it worked on postman, you can just press the code button on the right hand side. This will provide a working example in multiple languages. It is the button above the information icon. I would post a screenshot of it, but I don't have 10 reputation to do so.
i have found this good for my scenario , it is working perfect,
var client = new RestClient("https://test.salesforce.com/services/oauth2/token");
var request = new RestRequest(Method.POST);
request.AddHeader("content-type", "application/x-www-form-urlencoded");
request.AddParameter("application/x-www-form-urlencoded", "grant_type=password&client_id=3MVG9U_dUptXGpYKew7P.oPwrIsvowP_K4CsnkxHJIEOUJzW0XBUUY3o12bLDasjeIPGVobvBZo8TNFcCY6J3&client_secret=3189542819149073716&username=integraciones%40lamarina.com.mx.dev&password=2Password!4iwZvMQKVAwkYyJRy50JlAHk", ParameterType.RequestBody);
IRestResponse response = client.Execute(request);
Console.WriteLine("Response.StatusCode: " + response.StatusCode);
Console.WriteLine("Response.Content: " + response.Content);
Console.WriteLine("Response.ErrorMessage: " + response.ErrorMessage);
https://dotnetfiddle.net/J64FR5
in my case this is what worked
req.AddParameter("client_id", "unigen-corporation", ParameterType.HttpHeader);
req.AddParameter("grant_type", "client_credentials", ParameterType.GetOrPost);
If you've copied the code from the postman, try removing the following:
request.AlwaysMultipartFormData = true;
In my case after removing this line code worked.
var client1 = new RestClient(URI);
var request1 = new RestRequest(Method.POST);
request1.AddHeader("Content-Type", "application/x-www-form-urlencoded");
request1.AddParameter("client_id", "XX");
request1.AddParameter("client_secret", "XX");
request1.AddParameter("grant_type", "XX");
request1.AddParameter("role", "XX");
IRestResponse response1 = client1.Execute(request1);
System.Console.WriteLine(response1.Content);
Add parameters according to your needs. This work fine!
for my code, this works perfect.
// extention function for object->formed data body string
public static string toFormDataBodyString(this object src)
{
var res = new List<string>();
foreach (var key in src.GetType().GetProperties())
{
res.Add($"{key.Name}={src.GetType().GetProperty(key.Name)?.GetValue(src)}");
}
return string.Join("&", res);
}
//--------------------------------------
var data = new {
param1 = xxxx,
param2 = xxxx
}
var translateReq = new RestRequest("url_here")
.AddHeader("Content-Type", "application/x-www-form-urlencoded")
.AddParameter("application/x-www-form-urlencoded", data.toFormDataBodyString(), ParameterType.RequestBody);

Categories

Resources