Error: Value cannot be null. (Parameter 'accessToken')

When building the Square Client I get the
error Value cannot be null. (Parameter ‘accessToken’)
square API is 38.2 (latest)

using Square;
using Square.Exceptions;
using Square.Models;

           var client = new SquareClient.Builder()
               .Environment(Square.Environment.Sandbox)
               .AccessToken("Cut-and-Pasted-my-sandbox-token-here")
               .Build();

:wave: I’m Square Dev AI, here to help answer your question!
Based on the information you’ve provided, the following documentation will help answer your question:

Additional Documentation

Access Tokens and Other Credentials
Download, Configure, and Run the Client Sample
Get Developer Credentials

If this documentation helped you find a solution, please check the box indicating that this reply solves the problem.
Otherwise one of our Developer Advocates will provide additional assistance shortly.

I have even recreated the access token, but get the same result.

This error typically occurs when the access token provided to the SquareClient builder is null or empty. Make sure that the access token you’re using is valid and correctly pasted. If you’re using a variable to store the access token, ensure that the variable is properly initialized with the token value before it’s used in the SquareClient builder.

If you’re still facing issues, it might be a good idea to check if there are any leading or trailing spaces in your access token string that might be causing the problem.

Remember, you should never hardcode your access token in your code. It’s recommended to use environment variables or some form of secure configuration to store and retrieve your access tokens.

This answer was reviewed by @Bryan-Square.

hmm. lets check the things you suggested.

  1. Is it blank or empty?
    No. I did say in my post i cut and pasted it.

  2. Is it valid. Well I cut and pasted it from the credentials screen. I have even recreated it on the credentials using the button there. Surely it should be giving an error like invalid access token. anything but null or empty?

  3. Are there any leading or trailing zeros?
    No. The access token is exactly as it is displayed on my credentials screen.

  4. Thanks for reminding me about not hardcoded the access token. But I was just making the example code as simplistic as possible to demonstrate the error.

again here is my code with the access token removed.

using Square;
using Square.Exceptions;
using Square.Models;
            var client = new SquareClient.Builder()
                .Environment(Square.Environment.Sandbox)
                .AccessToken("REMOVED")
                .Build();

Are there any humans who work at square that can help? This must be pretty basic stuff.

the soloution was.

                var client = new SquareClient.Builder()
               .BearerAuthCredentials(
                    new BearerAuthModel.Builder(
                        "Cut-and-paste-your-token-here"
                    )
                    .Build())
               .Environment(Square.Environment.Sandbox)
               .Build();

Glad to see that you figured out the issue. :slightly_smiling_face: