读书人

关于.NET对接淘宝API的有关问题

发布时间: 2013-07-16 22:38:05 作者: rapoo

关于.NET对接淘宝API的问题
本帖最后由 xuzhenshun 于 2013-07-02 09:48:19 编辑 最近在学着对接淘宝的API

在登录那里被被搞晕了,下面是淘宝的说明
http://open.taobao.com/doc/detail.htm?spm=0.0.0.0.sIg3jQ&id=118

我是使用Server-side flow方法来获取

(1) 通过用户授权获取授权码Code; (获取授权码 :https://oauth.taobao.com/authorize ;沙箱访问 https://oauth.tbsandbox.com/authorize )


//沙箱环境
protected string AppKey = "102223556992";
protected string AppSecret = "sand3xc00f28d1f022ae6343ed69f5d";


protected void Page_Load(object sender, EventArgs e)
{
string url = "https://oauth.tbsandbox.com/authorize"; //沙箱环境
//参数
taobao.Model.parameterCode model = new Model.parameterCode()
{
client_id = AppKey,
redirect_uri = "localhost:50138/OAuth.aspx",
response_type = "code",
state = "13",
view = "web"
};

url += ("?client_id=" + model.client_id + "&redirect_uri=" + model.redirect_uri + "&response_type=" + model.response_type + "&state=" + model.state + "&view=" + model.view);

Response.Redirect(url);
}


这个第一步我已经完成了,然后完成后淘宝自动呆着code回转到localhost:50138/OAuth.aspx这个页面,这个页面的CS代码如下第二步所释。

(2) 用上一步获取的Code和应用密钥(AppSecret)通过Https Post方式换取Token。 (获取访问令牌: https://oauth.taobao.com/token ; 沙箱访问 https://oauth.tbsandbox.com/token )


//沙箱环境
protected string AppKey = "102223556992";


protected string AppSecret = "sand3xc00f28d1f022ae6343ed69f5d";

protected void Page_Load(object sender, EventArgs e)
{
if (Request.QueryString["code"] != null)
{
//获取到授权码Code,执行获取Access ToKen方法
GetAccessToKen(Request.QueryString["code"].ToString());
}
}

//获取Access ToKen令牌方法
protected void GetAccessToKen(string code)
{
string url = "https://oauth.tbsandbox.com/token"; //沙箱环境

#region 参数整合
taobao.Model.parameterAccessToKen model = new Model.parameterAccessToKen()
{
client_id = AppKey,
client_secret = AppSecret,
code = code,
grant_type = "authorization_code",
redirect_uri = "localhost:50138/OAuth.aspx",
state = "13",
view = "web"
};

string strCan = "";
strCan += ("client_id=" + model.client_id);
strCan += ("&client_secret=" + model.client_secret);


strCan += ("&code=" + model.code);
strCan += ("&grant_type=" + model.grant_type);
strCan += ("&redirect_uri=" + model.redirect_uri);
strCan += ("&state=" + model.state);
strCan += ("&view=" + model.view);
#endregion

Response.Write(GetPage(url, strCan));

}


/// <summary>
/// Post数据到网站
/// </summary>
/// <param name="posturl">网址</param>
/// <param name="postData">参数</param>
/// <returns></returns>
public string GetPage(string posturl, string postData)
{
Stream outstream = null;
Stream instream = null;
StreamReader sr = null;
HttpWebResponse response = null;
HttpWebRequest request = null;
Encoding encoding = System.Text.Encoding.GetEncoding("UTF-8");
byte[] data = encoding.GetBytes(postData);
// 准备请求...
try
{
// 设置参数


request = WebRequest.Create(posturl) as HttpWebRequest;
CookieContainer cookieContainer = new CookieContainer();
request.CookieContainer = cookieContainer;
request.AllowAutoRedirect = true;
request.Method = "POST";
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = data.Length;
outstream = request.GetRequestStream();
outstream.Write(data, 0, data.Length);
outstream.Close();
//发送请求并获取相应回应数据
response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
instream = response.GetResponseStream();
sr = new StreamReader(instream, encoding);
//返回结果网页(html)代码
string content = sr.ReadToEnd();
string err = string.Empty;
return content;
}
catch (Exception ex)
{


string err = ex.Message;
return err;
}
}




但是,这个第二部怎么都弄好不!尤其是“通过Https Post方式换取Token”我都改了好多种POST的方法了,还是不行,每次都会报错“基础连接已经关闭: 未能为 SSL/TLS 安全通道建立信任关系。”
淘宝API .NET对接淘宝API Access?Token session?key 沙箱环境
[解决办法]
人家用的ssl/tls

//如果是发送HTTPS请求
if(url.StartsWith("https",StringComparison.OrdinalIgnoreCase))
{
ServicePointManager.ServerCertificateValidationCallback = new RemoteCertificateValidationCallback(CheckValidationResult);
request = WebRequest.Create(url) as HttpWebRequest;
request.ProtocolVersion=HttpVersion.Version10;
}

你post加上这个

读书人网 >Web Service

热点推荐