拼多多使用code获取access_token

//获取访问令牌
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啦 只需要稍作修改。

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

经验分享 程序员 微信小程序 职场和发展