非静态字段 Ebay oauth 库需要一个对象引用

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

所以我引用了 ebay.oauth.client 库,我试图用它来获取访问令牌,但它给了我一个错误。

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

这是我的代码

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;
        }

好的,我明白了,在我的 class 顶部,我需要这样的静态 oauthapi:

private static OAuth2Api api;

然后我需要像这样获取应用程序令牌

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

为了帮助所有遇到此类问题的用户,提供以下源自您的代码是有用的...

using System;
using System.Collections.Generic;
using System.Windows.Forms;
using eBay.ApiClient.Auth.OAuth2;
using eBay.ApiClient.Auth.OAuth2.Model;
private static OAuth2Api api = new OAuth2Api();
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 = api.GetApplicationToken(env, scopes).AccessToken.Token;
    return oAuthToken;
}