Abdul Ali
Abdul Ali

Reputation: 1937

xero c# API not working

have copied sample xero API wrapper in C# from github. But when same code is copied into the current MVC3 project, it gives a Object reference not set to an instance of state .

the relevant code is as follows:

Home controller method:

public ActionResult Index()
       {

            IOAuthSession oauthSession = ServiceProvider.GetCurrentSession();
            Repository repository = ServiceProvider.GetCurrentRepository();

            // Can we already access an organisation??
            if (oauthSession.HasValidAccessToken && repository != null && repository.Organisation != null)
            {
                return new RedirectResult("~/");
            }

            if (oauthSession is XeroApiPrivateSession)
            {
                throw new ApplicationException("The current private session cannot access the authorised organisation. Has the access token been revoked?");
            }


            // Determine the callback uri to use - this must match the domain used when the application was registerd on http://api.xero.com
            var callbackUri = new UriBuilder(Request.Url.Scheme, Request.Url.Host, Request.Url.Port, Url.Action("Callback"));


            // Call: GET /oauth/RequestToken

   RequestToken requestToken = oauthSession.GetRequestToken(callbackUri.Uri);
// error comes here...            
            string authorisationUrl = oauthSession.GetUserAuthorizationUrl();

            return new RedirectResult(authorisationUrl);
        }

the full error is as follows:

Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error: 


Line 45: 
Line 46:             // Call: GET /oauth/RequestToken
Line 47:             RequestToken requestToken = oauthSession.GetRequestToken(callbackUri.Uri);
Line 48:             
Line 49:             string authorisationUrl = oauthSession.GetUserAuthorizationUrl();

Source File: \Xero\Xero\Xero\Xero\Controllers\HomeController.cs    Line: 47 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   DevDefined.OAuth.Consumer.OAuthSession.GetRequestToken(Uri callbackUri) in n:\w2\861c2b03515ac7b6\source\XeroApi\OAuth\Consumer\OAuthSession.cs:181
   Xero.Controllers.HomeController.Index() in i:\Softech projects\Xero\Xero\Xero\Xero\Controllers\HomeController.cs:47
   lambda_method(Closure , ControllerBase , Object[] ) +101
   System.Web.Mvc.ActionMethodDispatcher.Execute(ControllerBase controller, Object[] parameters) +59
   System.Web.Mvc.ReflectedActionDescriptor.Execute(ControllerContext controllerContext, IDictionary`2 parameters) +435
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethod(ControllerContext controllerContext, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +60
   System.Web.Mvc.<>c__DisplayClass15.<InvokeActionMethodWithFilters>b__12() +145
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodFilter(IActionFilter filter, ActionExecutingContext preContext, Func`1 continuation) +433
   System.Web.Mvc.<>c__DisplayClass17.<InvokeActionMethodWithFilters>b__14() +72
   System.Web.Mvc.ControllerActionInvoker.InvokeActionMethodWithFilters(ControllerContext controllerContext, IList`1 filters, ActionDescriptor actionDescriptor, IDictionary`2 parameters) +323
   System.Web.Mvc.ControllerActionInvoker.InvokeAction(ControllerContext controllerContext, String actionName) +844
   System.Web.Mvc.Controller.ExecuteCore() +130
   System.Web.Mvc.ControllerBase.Execute(RequestContext requestContext) +229
   System.Web.Mvc.ControllerBase.System.Web.Mvc.IController.Execute(RequestContext requestContext) +39
   System.Web.Mvc.<>c__DisplayClassb.<BeginProcessRequest>b__5() +71
   System.Web.Mvc.Async.<>c__DisplayClass1.<MakeVoidDelegate>b__0() +44
   System.Web.Mvc.Async.<>c__DisplayClass8`1.<BeginSynchronous>b__7(IAsyncResult _) +42
   System.Web.Mvc.Async.WrappedAsyncResult`1.End() +152
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +59
   System.Web.Mvc.Async.AsyncResultWrapper.End(IAsyncResult asyncResult, Object tag) +40
   System.Web.Mvc.<>c__DisplayClasse.<EndProcessRequest>b__d() +38
   System.Web.Mvc.SecurityUtil.<GetCallInAppTrustThunk>b__0(Action f) +31
   System.Web.Mvc.SecurityUtil.ProcessInApplicationTrust(Action action) +61
   System.Web.Mvc.MvcHandler.EndProcessRequest(IAsyncResult asyncResult) +118
   System.Web.Mvc.MvcHandler.System.Web.IHttpAsyncHandler.EndProcessRequest(IAsyncResult result) +38
   System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +9514812
   System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +155

any help appreciated.

the aim is to connect to xero api and use its methods. if there is any working methods for C#, please inform if possible

Upvotes: 0

Views: 1437

Answers (1)

Allen King
Allen King

Reputation: 2516

The line throwing the exception is used for obtaining unverified request token from Xero. Without knowing much what you have done to setup the app, it appears that you have not registered your app with Xero and have not added your consumer key and secret in Web.config.

For a Xero public app, these settings are required:

<!-- Consumer Key and Secret, taken from an public/partner application registered at http://api.xero.com -->
    <add key="XeroApiConsumerKey" value="xxxxxxxxxxxx"/>
    <add key="XeroApiConsumerSecret" value="xxxxxxxxxxxxxxxxxxx"/>
    <!-- SignatureMethod. Use 'HMAC-SHA1' for public apps, 'RSA-SHA1' for private and partner apps. -->
    <add key="XeroApiSignatureMethod" value="HMAC-SHA1"/>
    <!-- Xero API Endpoints 
      Use 'https://api.xero.com/' for public and private apps
      Use 'https://api-partner.network.xero.com/' for partner apps
    -->
    <add key="XeroApiBaseUrl" value="https://api.xero.com/"/> 

In your app setup at Xero developer website, make sure to use right domain name. For example, if you are running your app on your local host (say for testing), enter localhost in domain name field in your registered app at Xero.

If all these settings are properly made, there is no reason why it wouldn't work. You can ask me if you run into other issues.

[By the way, referring to the exception you have posted, you can always open Xero API source and find out what is line 181 of OAuthSession.cs so that you know what object is not being populated.]

Upvotes: 1

Related Questions