关于.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);
}
//沙箱环境
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;
}
}