.net 非静态字段Ebay oauth库需要对象引用

v8wbuo2f  于 2023-05-01  发布在  .NET
关注(0)|答案(2)|浏览(128)

所以我参考了eBay。客户端库,我试图使用它来获取访问令牌,但它给我一个错误。
非静态字段、方法或属性“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;
        }
wxclj1h5

wxclj1h51#

好吧,我想明白了,在我的类的顶部,我需要静态oauthapi,如下所示:

private static OAuth2Api api;

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

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

q9yhzks02#

为了帮助所有用户的这种问题,这是有用的,给予代码如下源于你的...

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

相关问题