因爲本人剛畢業進公司實習 遇到一些問題而後想經過博客來記錄和分享給你們一塊兒學習。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
一、軟件代碼爲原創,如需轉載,請註明出處;學習
二、若是文中有什麼錯誤,歡迎指出,謝謝!測試