Search code examples
c#.netasp.net-mvcebay-api

An object reference is required for the non-static field Ebay oauth library


So I have referenced the ebay.oauth.client library and I'm trying to use it to get the access token but it's giving me an error.

An object reference is required for the non-static field, method or property 'OAuth2Api.GetApplicationToken(OAuthEnvironment, IList)

Here is my code

private string GetAuthTokenEbay()
        {
            bool isTestSite = true;
            var url = (isTestSite) ? "https://api.sandbox.ebay.com" : "https://api.ebay.com"
            var oAuthToken = "";
            CredentialUtil.Load("config.yaml");
            IList<String> scopes = new List<String>()
            {
                url + "/oauth/api_scope/buy.marketing",
                url + "/oauth/api_scope",
                url + "/oauth/api_scope/sell.inventory.readonly",
                url + "/oauth/api_scope/sell.inventory"
            };
            //String state = "current-page";
            OAuthEnvironment env = (isTestSite) ? OAuthEnvironment.SANDBOX : OAuthEnvironment.PRODUCTION;
            oAuthToken = OAuth2Api.GetApplicationToken(env, scopes).AccessToken;

            return oAuthToken;
        }

Solution

  • Ok I figured it out, at the top of my class I needed the static oauthapi like so:

    private static OAuth2Api api;
    

    then I needed to get the application token like so

    oAuthToken = api.GetApplicationToken(env, scopes).AccessToken.Token;