eBay账号token授权

时间:2024-01-22 19:07:32

1、注册开发者账号(https://go.developer.ebay.com/) 

  hufangyong   hu6253859.

2、注册沙箱测试账号(http://sandbox.ebay.com)

      testuser_hufangyong  hu6253859.

3、注册好开发者账号后登陆(如下图),这里需要记录AppID,DevID,CertID。后面API接口请求Token时需要用到,然后点击 User Tokens

 

4、我们这里选择了Sandbox沙箱环境,所以我们登陆沙箱账号,将沙箱测试账号绑定到开发者账号,然后生成一个授权token

 

 

5、登陆了沙箱测试账号后,手动生成的token就已经有了,测试时可以直接copy这个token用

 

6、打开下面的Get a token from eBay via  Your Application 列表,记录runame,动态获取SessionId时要用到

 

 

7、通过上面的手动获取token的过程,我们已经记录了开发者账号的AppID,DevID,CertID,RuName这四个参数。这是在下面通过API接口动态获取Token的必要参数。

根据AppID,DevID, CertID,RuName获取SessionId,然后再拼接成用户登陆验证授权的URL。

  public string GetSessionId()

        {

            ApiContext localContext = new ApiContext();

            ApiAccount apiAccount = new ApiAccount();

            apiAccount.Application = AppID;

            apiAccount.Developer = DevID;

            apiAccount.Certificate = CertID;

 

            localContext.ApiCredential.ApiAccount = apiAccount;

            localContext.SoapApiServerUrl="https://api.sandbox.ebay.com/wsapi";

            localContext.Version = "981";

            localContext.Timeout = 50000;

            localContext.Site = SiteCodeType.US;

            localContext.ErrorLanguage = ErrorLanguageCodeType.zh_CN;

 

            GetSessionIDCall apiCall = new GetSessionIDCall(localContext);

            apiCall.RuName = RuName;

            string sessionId = apiCall.GetSessionID();      

    return sessionId; //https://signin.sandbox.ebay.com/ws/eBayISAPI.dll?SignIn&runame="+runame+"&SessID=" + sessionId

        }

 

8、拼接好URL后,需要用户登陆验证授权(激活这个SessionId),可以粘贴上述地址到浏览器上,也可以在程序中打开,然后用户点击Agree同意授权  

 

9、在用户登陆验证激活SessionId后,就可以通过SessionId来获取Token了

  public  void GetUserTokenBySessionId ( string sessionId)

        {

 

            ApiContext localContext = new ApiContext();

            ApiAccount apiAccount = new ApiAccount();

            apiAccount.Application = application;

            apiAccount.Developer = developer;

            apiAccount.Certificate = certificate;

           

            localContext.ApiCredential.ApiAccount=apiAccount;

            localContext.RuName = runame;

            localContext.SoapApiServerUrl="https://api.sandbox.ebay.com/wsapi";

            localContext.Version = "981";

            localContext.Timeout = 50000;

            localContext.Site = SiteCodeType.US;

            localContext.ErrorLanguage = ErrorLanguageCodeType.zh_CN;

 

            FetchTokenCall apiCall = new FetchTokenCall(localContext);

            apiCall.SessionID = sessionId;

            try

            {

                string token = apiCall.FetchToken(sessionId);

                _output.WriteLine("TOKEN:"+token);

            }

            catch (Exception e)

            {

                _output.WriteLine("TOKEN:" + e.Message);

            }

        }

 end