public AliExpressAccessToken getAccessToken(string code)
        {
            ITopClient client             = new DefaultTopClient(StaticValues.aliURL, StaticValues.aliAppkey, StaticValues.aliSecret);
            TopAuthTokenCreateRequest req = new TopAuthTokenCreateRequest();

            req.Code = code;
            TopAuthTokenCreateResponse rsp         = client.Execute(req);
            AliExpressAccessToken      accessToken = JsonConvert.DeserializeObject <AliExpressAccessToken>(rsp.TokenResult);

            return(accessToken);
        }
示例#2
0
        private void btnGetToken_Click(object sender, EventArgs e)
        {
            try
            {
                int type = 1;
                if (type == 1)
                {
                    #region 方式1
                    WebUtils webUtils = new WebUtils();
                    IDictionary <string, string> pout = new Dictionary <string, string>();
                    pout.Add("grant_type", "authorization_code");
                    pout.Add("client_id", appkey);
                    pout.Add("client_secret", secret);
                    pout.Add("code", code);
                    pout.Add("redirect_uri", callbackUrl);
                    string output = webUtils.DoPost("https://oauth.taobao.com/token", pout);
                    Console.Write(output);

                    if (!string.IsNullOrEmpty(output.ToString()))
                    {
                        JavaScriptSerializer Serializers = new JavaScriptSerializer();
                        Root objroot = Serializers.Deserialize <Root>(output.ToString());
                        txtJson.Text = objroot.refresh_token;

                        string      xmlPath = "./Configxml/configtaobao.xml";
                        XmlDocument doc     = new XmlDocument();
                        doc.Load(xmlPath);
                        XmlNode xn = doc.SelectSingleNode("//session");
                        xn.InnerText = objroot.refresh_token;
                        sessionKey   = objroot.refresh_token;
                        doc.Save(xmlPath);
                    }
                    #endregion
                }
                else if (type == 2)
                {
                    #region 方式2
                    ITopClient client             = new DefaultTopClient(url, appkey, secret, "json");
                    TopAuthTokenCreateRequest req = new TopAuthTokenCreateRequest();
                    req.Code = code;
                    req.Uuid = ""; //可选
                    TopAuthTokenCreateResponse rsp = client.Execute(req);
                    Console.WriteLine(rsp.Body);
                    // txtBoy.Text = rsp.Body;
                    txtJson.Text = rsp.Body;

                    JavaScriptSerializer Serializers = new JavaScriptSerializer();
                    //string strTest='{"top_auth_token_create_response":{"token_result":"{\"w1_expires_in\":1800,\"refresh_token_valid_time\":1526285728986,\"taobao_user_nick\":\"b01GflzbxtAIf8KwrSO9nNQofbFFz2kii4lSP%2FHJy9oC5c%3D\",\"re_expires_in\":0,\"expire_time\":1534061728986,\"open_uid\":\"AAE3PSJFAGQsSvMnMnzd485d\",\"token_type\":\"Bearer\",\"access_token\":\"6201321761ca692cc4cdeccc132acegff62acee1e8c41771837606616\",\"taobao_open_uid\":\"AAE3PSJFAGQsSvMnMnzd485d\",\"w1_valid\":1526287528986,\"refresh_token\":\"6201121d1ef9388da8ba2d6e48e3dfh4af13ac9198572431837606616\",\"w2_expires_in\":0,\"w2_valid\":1526285728986,\"r1_expires_in\":1800,\"r2_expires_in\":0,\"r2_valid\":1526285728986,\"r1_valid\":1526287528986,\"expires_in\":7776000}","request_id":"165an4c9a92sp"}} ';

                    //json字符串转为数组对象, 反序列化
                    TaobaoRoot objs = Serializers.Deserialize <TaobaoRoot>(rsp.Body.ToString());
                    if (objs != null)
                    {
                        if (objs.top_auth_token_create_response != null)
                        {
                            string strroot = objs.top_auth_token_create_response.token_result;
                            Root   objroot = Serializers.Deserialize <Root>(strroot);
                        }
                    }
                    #endregion
                }
            }
            catch (Exception ex)
            {
                WriteExceptionLog(ex);
            }
        }