使用code获取access_token
最近公司准备做一个拼多多开放平台里的api接口调用去查看商家的订单、商品、物流等等。所以需要code去换取access_token,因为我也是第一次接触的这个api的调用 有很多的不懂 然后就上了百度搜了一会 发现都没有拼多多的案例什么的。然后就看到了微信跟拼多多的很类似,所以就参考了一下。
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30//获取访问令牌 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啦 只需要稍作修改。
然后就可以到拼多多开放平台里面的控制台下的测试工具进行测试 然后就可以看到它返回的结果是什么了。
最后
以上就是风趣火车最近收集整理的关于拼多多使用code获取access_token的全部内容,更多相关拼多多使用code获取access_token内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复