因为本人刚毕业进公司实习 遇到一些问题而后想经过博客来记录和分享给你们一块儿学习。html
第一次写博客没什么经验不是写的很好 请你们多多关照 嘴下留情哈哈 谢谢!json
好了 话很少说直接进入主题。api
最近公司准备作一个拼多多开放平台里的api接口调用去查看商家的订单、商品、物流等等。因此须要code去换取access_token,由于我也是第一次接触的这个api的调用 有不少的不懂 而后就上了百度搜了一会 发现都没有拼多多的案例什么的。而后就看到了微信跟拼多多的很相似,因此就参考了一下。微信
//获取访问令牌 string postUrl="http://open-api.pinduoduo.com/oauth/token"; string strResponse; string strFormValues; HttpWebRequest myHttpWebRequest=(HttpWebRequest)WebRequest.Create(postUrl); myHttpWebRequest.Method="POST"; myHttpWebRequest.ContentType="application/json"; //将参数存放在Dictionary<string,string>里面 再转化成json 进行请求 Dictionary<string,string> dic=new Dictionary<string,string>(); dic.Add("grant_type","authorization_code"); dic.Add("code","[用户登陆受权后获取的code]"); dic.Add("client_id","[应用建立时的client_id]"); dic.Add("client_secret","[应用建立时的client_secret]"); dic.Add("redirect_uri","[应用建立时的回调地址]"); string json=(new JavaScriptSerializer()).Serialize(dic); ASCIIEncoding encoding=new ASCIIEncoding(); byte[] byte1=encoding.GetBytes(json); strFormValues=Encoding.ASCII.GetString(byte1); myHttpWebRequest.ContentLength=strFormValues.Length; //发送请求 StreamWriter stOut=new StreamWriter(myHttpWebRequest.GetRequestStream(),Encoding.ASCII); stOut.Write(strFormValues); stOut.Close(); //接受返回信息 StreamReader stIn=new StreamReader(myHttpWebRequest.GetResponse().GetResponseStream()); strResponse=stIn.ReadToEnd(); stIn.Close(); return strResponse;
这样就能够获取到access_token啦 只须要稍做修改。app
而后就能够到拼多多开放平台里面的控制台下的测试工具进行测试 而后就能够看到它返回的结果是什么了。工具
注:post
一、软件代码为原创,如需转载,请注明出处;学习
二、若是文中有什么错误,欢迎指出,谢谢!测试