简体   繁体   中英

cordova project gives unsupported_grant_type error but same works on browser

I have a ASP.Net WebAPI2 website that has a /token implementing the OWIN token service. This service is working just fine for my website, I am able to use rest clients and get the token as expected both from my PC as well as Android Phone.

however when I call the service from inside my ionic project deployed on my android phone KitKat OS, i get {"error":"unsupported_grant_type"} . Its on the same wifi so I am able to use a rest client and get the token using that.

here is the angular code that I am using:

.service('LoginTokenService', ['$resource', function ($resource) {
    function getToken(payLoad, fnSuccess, fnError) {
        return $resource("http://192.168.1.104/token", {},
      {
          "getAuthToken": {
              method: "POST",
              isArray: false,
              headers: { 'Content-Type': 'application/x-www-form-urlencoded' },

          }
      })
    .getAuthToken(payLoad, fnSuccess, fnError);
    }
    return ({
        getToken: getToken
    });

Also on my webapi code I have set the option app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll); like below

    public void ConfigureAuth(IAppBuilder app)
    {

        app.UseCors(Microsoft.Owin.Cors.CorsOptions.AllowAll);

        // Mounts the middleware on the provided app with the options configured
        // above
        app.UseOAuthBearerTokens(OAuthOptions);
    }

Also the payLoad contains UserName='myuser@domain.com', Password='my_password' & grant_type='password'

as I am relatively new to ionic and cardova I was wondering if anyone has encountered the similar issue.

I think I know the issue. on my Website, i used $.param(payload) to send login info to the /token service, but as JQuery was not available in the ionic by default, I removed that.

I have now used the $httpParamSerializerJQLike(payLoad) to serialize the payload and its working now..

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM