Xero - beautiful accounting software

Xero Developer Help Center

Xero Developer Community

Community > Getting Started >

Public Authenticator

Started by James Borland -   in Getting Started

I've cut & pasted the PublicAuthenticator.cs code into a project. The using statements at the top of the project are as follows and all look fine:

using System;
using System.Diagnostics;
using Xero.Api.Infrastructure.Interfaces;
using Xero.Api.Infrastructure.OAuth.Signing;
using Xero.Api.Infrastructure.ThirdParty.Dust

I then have a line like:
public class PublicAuthenticator : TokenStoreAuthenticator

And I get "The type or namespace name 'TokenStoreAuthenticator' could not be found (are you missing a using directive or an assembly reference).

I have other errors too but I'm sure they all relate to the same thing.


This question is more of a general programming question.

The line
public class PublicAuthenticator : TokenStoreAuthenticator
Says that the PublicAuthenticator inherits the TokenStoreAuthenticator. The error message you get is a typical error saying that the TokenStoreAuthenticator class cannot be found. I.e. you have not included it in your project.

Note that the TokenStoreAuthenticator is not part of the XeroApi .Net wrapper which you have included in the usings.

If you look at the sample code structure of the newer Xero Api and navigate to:
Examples -> Xero.Api.Example.Application -> TokenStoreAuthenticator.cs - this is the class you are missing.

Long story short, since you've probably (I'm guessing) created a completely new project and copied over the PublicAuthenticator class, you will also need to copy stuff it references (that is not inside the Xero.Api project), such as the TokenStoreAuthenticator.cs.

More precisely, the file can be seen here (look for 'TokenStoreAuthenticator.cs'), Create this class and copy its code.

Serge P  

Thanks for that I'm a little bit further on.
The PublicAuthenticator takes 4 parameters, 3 strings and an ITokenStore. How do I use the ITokenStore. I've defined it as ITokenStore iTokenStore but I've no idea how to assign it a value or where to get that value from.
So I'm getting an error: "Use of unassigned local variable 'iTokenStore' and obviousy I need to get a value in there anyway.

How do I use the IToken variable? There's probably some documentation on what should be in all the variable but I'm struggling to find it just now.


Accounts Department  

I am able to Authenticate with the older API the one with using references like:

using XeroApi.OAuth;

But I'm assuming that one is being phased out that's why it's no longer supported. So I'm try to use the newer API the one using references like:

using Xero.Api.Infrastructure.OAuth;

I totally stuck with the PublicAuthenticator.

PublicAuthenticator(string baseUri, string tokenUri, string callBackUrl, iTokenStore);

How do I assign a value to iTokenStore?

Thanks in advance for your help.

Accounts Department  

The more recent Wrapper (Xero.Api) gives you access to (apart from other additions) more entities (such as BankTransfers, PdfFiles, RepeatingInvoices, Setup) and is being updated from time to time, so I would recommend using this wrapper in the long run.

The sample code provides a functional example. Instead of trying to make a new project just download the sample and learn from it first. To set it up with your public app all you really need to do is change the consumer key and secret in the App.config file. For the public app these are located in:
Examples -> Xero.Api.Example.Counts -> App.config

It should look something like this:
<add key="BaseUrl" value="https://api.xero.com"/>
<add key="ConsumerKey" value="XXX.."/>
<add key="ConsumerSecret" value="XXX.."/>
<add key="CallbackUrl" value="oob"/>

If you want to go ahead and create a new project you could work off classes already defined for you in the sample

Particularly, when you create a new project you may need/should start of with (apart from referencing the Xero.Api.dll project) these classes:
Examples -> Xero.Api.Example.Applications -> TokenStoreAuthenticator.cs
Examples -> Xero.Api.Example.Applications -> Public -> AmericanPayroll.cs
Examples -> Xero.Api.Example.Applications -> Public -> AustralianPayroll.cs
Examples -> Xero.Api.Example.Applications -> Public -> Core.cs
Examples -> Xero.Api.Example.Applications -> Public -> PublicAuthenticator.cs
Examples -> Xero.Api.Example.Applications -> Public -> Settings.cs
Examples -> Xero.Api.Example.Counts -> Program.cs
(main class)
Examples -> Xero.Api.Example.Counts -> Lister.cs (important class)
*Examples -> Xero.Api.Example.TokenStores -> MemoryTokenStore.cs
*Examples -> Xero.Api.Example.TokenStores -> SqliteTokenStore.cs

Files marked * means you need one or the other depending on the method of Storing you use for the tokens. This can be changed at line 13 of Examples -> Xero.Api.Example.Counts -> Program.cs which looks like:
var tokenStore = new SqliteTokenStore();

Also be sure to copy the necessary App.config info from:
Examples -> Xero.Api.Example.Counts -> App.config

You don't necessarily need all of the above files, but as a starter while you're learning may as well reference them all in your new project.

Serge P  

Hi Serge,
Thanks for the help. The only download I see is the "Download ZIP" button on the first page and that downloads "Xero-Net-master.zip". Is that the correct one to download?

I was looking for individual little projects to show how to do specific things.


James Borland  


I too am confused about the usage of the ITokenStore object. The sample code has many constructors with this object as a required parameter, but not a example actually showing how to create, say an instance of the Core class public Core(ITokenStore store, IUser user). How do you assign a value to the ITokenStore?



Yohancef Chin  

Apparently you can create a class that inherits ITokenStore (MyTokenStore : ITokenStore) and pass that object into the constructor.

Yohancef Chin