Get Access Token with BreezeSharp

399 Views Asked by At

I need to use EntityQuery in BreezeSharp to get Access Token from my Breeze WebAPI

I have a class called TokenResponseModel for deserializing my json from the server as follows:

using Newtonsoft.Json;
namespace CMIS.Integration.Common
{
    class TokenResponseModel
    {
        [JsonProperty("access_token")]
        public string AccessToken { get; set; }

        [JsonProperty("token_type")]
        public string TokenType { get; set; }

        [JsonProperty("expires_in")]
        public int ExpiresIn { get; set; }

        [JsonProperty("userName")]
        public string Username { get; set; }

        [JsonProperty(".issued")]
        public string IssuedAt { get; set; }

        [JsonProperty(".expires")]
        public string ExpiresAt { get; set; }
    }
}

I have the following code to run:

EntityQuery query=EntityQuery.From("Token",new TokenResponseModel()).
WithParameters(new Dictionary<string,object>{{"grant_type","password"},{"username","my_username"},{"password","my_password"}});

EntityManager mng = new EntityManager(baseUrl);

var tokenobject = await query.Execute(mng);

When I run it, I Get an error. It requires metadata which is not there for the "/Token" method on the server.

How Can I call it with BreezeSharp.

With RestSharp I can do it as follows:

RestRequest request = new RestRequest("/Token", Method.POST);

request.AddParameter("grant_type", "password");
request.AddParameter("username", "my_username");
request.AddParameter("password", "my_password");

RestClient client = new RestClient(baseUrl);
var response = client.Execute<AccessToken>(request);

And this works fine. Thanks

More Explanation: What I want to say is that sometimes I just need to get the result from the breeze server just i a JSON format. I don't want it mapped to any objects on the client. A good example is my case for authenticating a user using the Token method. I know how to parse the JSON myself. I just want breeze to bring the result from the call below:

string baseUrl = "http://myserver_url/NHIFService/";
EntityQuery query = EntityQuery.From<string>("Token").WithParameters(new new Dictionary<string, object> { { "grant_type", "password" }, { "username", "my_username" }, { "password", "my_password" } });

EntityManager mng = new EntityManager(baseUrl);
var tokenobject = await query.Execute(mng);

I want to be able to do this because sometimes I return anonymous objects from the server that do not have a match on the client or server. Can breese sharp allow me to do this without caring about the metadata. Or how can I supress metadata fetching.

Thank you.

1

There are 1 best solutions below

0
On

After going through the BreezeSharp Source Code I came with the solution for doing what I wanted. The guys at IdeaBlade have created this DataService class that enables returning RAW JSON from the server without even caring about the Metadata. Here is how I did it:

string token = await AuthenticationHelper.GetAccessToken();

string baseUrl = "http://my_server_url/appname/breeze/my_controller/";

DataService ds = new DataService(baseUrl);
string resourcePath = string.Format("GetCardDetails?CardNo={0}", cardNoTextEdit.EditValue);
ds.HttpClient.DefaultRequestHeaders.Add("Authorization", "Bearer " + token);
string result=await ds.GetAsync(resourcePath);

Congratulation guys Breeze Sharp is awesome.