拼多多使用code获取access_token
使用code获取access_token
最近公司准备做一个拼多多开放平台里的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啦 只需要稍作修改。

然后就可以到拼多多开放平台里面的控制台下的测试工具进行测试 然后就可以看到它返回的结果是什么了。

最后修改:2023 年 02 月 12 日
如果觉得我的文章对你有用,请随意赞赏