新聞中心
這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
創(chuàng)新互聯(lián)百度小程序教程:SessionKey
- Session Key
- 接口說明
- 接口地址
- 方法參數(shù)
- Header 參數(shù)
- post 參數(shù)
- 若參數(shù)無誤,服務器將返回一段 JSON 文本,包含以下數(shù)據(jù)
- 返回示例
- 若請求錯誤,服務器將返回一段 JSON 文本,包含以下參數(shù)
- 返回示例
- 錯誤碼
- 請求示例
Session Key
接口說明
智能小程序在其服務端中發(fā)送 POST 請求到百度 oauth2.0 授權服務地址,并帶上對應的參數(shù),便可獲取到 Session Key 。

接口地址
獲取 Session Key 的 URL 地址 :
https://spapi.baidu.com/oauth/jscode2sessionkey
為了讓您的智能小程序運行在聯(lián)盟 App 上,我們對獲取 SessionKey 的接口做了升級。新接口可以兼容獲取百度內(nèi)部 App 和百度外部 App 的 SessionKey。
您只需要將原接口地址
https://openapi.baidu.com/nalogin/getSessionKeyByCode
更改為
https://spapi.baidu.com/oauth/jscode2sessionkey
即可讓您的智能小程序在其它 App 上自動實現(xiàn)賬號關聯(lián)。
方法參數(shù)
Header 參數(shù)
| 參數(shù)名 | 類型 | 是否必須 | 描述 |
|---|---|---|---|
| Content-Type | Application/x-www-form-urlencoded | 是 | HTTP 的實體首部字段,瀏覽器原生 form 表單 |
post 參數(shù)
| 參數(shù)名 | 是否必須 | 說明 |
|---|---|---|
| code | 是 | 通過 swan.getLoginCode 獲取 Authorization Code 特殊說明:code 中有@符號時,會請求對應的開源宿主,用戶身份校驗及 SessionKey 生成過程由開源宿主實現(xiàn) |
| client_id | 是 | 智能小程序的 AppKey 智能小程序 AppKey 示例:4fecoAqgCIUtzIyA4FAPgoyrc4oUc25c |
| sk | 是 | 智能小程序的 AppSecret |
若參數(shù)無誤,服務器將返回一段 JSON 文本,包含以下數(shù)據(jù)
| 字段名 | 說明 |
|---|---|
| openid | 用戶身份標識,由 appid 和 uid 生成 不同用戶登錄同一個小程序獲取到的 openid 不同,同一個用戶登錄不同小程序獲取到的 openid 也不同 |
| session_key | 用戶的 Session Key |
返回示例
{openid: "l214zFqNrEuIEnp6m7Y01sw8yj",session_key: "981ce8b151c0599acf7ad1a673c6ff5e"}
若請求錯誤,服務器將返回一段 JSON 文本,包含以下參數(shù)
| 字段名 | 說明 |
|---|---|
| errno | 錯誤碼,詳情見下方錯誤碼 |
| error | 錯誤描述 |
| error_description | 錯誤描述信息,用來幫助理解和解決發(fā)生的錯誤 |
返回示例
{"errno": 10010100,"error": "parameter is invalid","error_description": "Key: 'Code2SessionKeyParam.ClientID' Error:Field validation for 'ClientID' failed on the 'required' tag"}
錯誤碼
| 錯誤碼 | 描述 | 錯誤原因自查 |
|---|---|---|
| 10010100 | 參數(shù)錯誤或 code 的值無效 | 1 請檢查使用 client_id 參數(shù)是否是 appKey 2 請檢查生成 code 和使用 code 的時間差是否超過了有效期,有效期為 10s 3 請檢查生成 code 的小程序的 appKey 和當前請求接口的 client_id 是否一致 |
| 10010400 | client_id 與 sk 不匹配 | 1 請聯(lián)系小程序管理員檢查小程序 appSecret 是否有修改 |
| 10010300 | 請求開源宿主失敗 | 1 信息為開源宿主返回,請參考 error_description 描述進行錯誤自查 |
請求示例
- PHP
- GOLANG
/*** 獲取sessionkey 方法* @param string $code 由swan.getLoginCode獲取的臨時登錄憑證* @param string $clientId 小程序appkey* @param string $sk 小程序appSecretKey*/function reqGetSessionkey($code, $clientId, $sk){$url = 'https://spapi.baidu.com/oauth/jscode2sessionkey';$data = array("code" => $code,"client_id" => $clientId,"sk" => $sk);$ret = curlPost($url, $data);return $ret;}/*** curl POST請求工具類** @param string $url* 請求的url地址* @param array $postDataArr* 傳遞的數(shù)組參數(shù)* @return string 檢測結果json字符串*/function curlPost($url, $postDataArr){$headerArr = array("Content-type:application/x-www-form-urlencoded");$curl = curl_init();curl_setopt($curl, CURLOPT_URL, $url);curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, FALSE);curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, FALSE);curl_setopt($curl, CURLOPT_POST, 1);curl_setopt($curl, CURLOPT_POSTFIELDS, $postDataArr);curl_setopt($curl, CURLOPT_HTTPHEADER, $headerArr);curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);$output = curl_exec($curl);curl_close($curl);return $output;}// 獲取sessionkey demoecho reqGetSessionkey("8ba01454ac57775d3692f5dbfcac7a28NW", "4fecoAqgCIUtzIyA4FAPgoyrc4oUc25c", "xxx");
package mainimport ("encoding/json""fmt""io/ioutil""log""net/http""net/url""strings""time")//返回數(shù)據(jù)結構type response struct {Errno int `json:"errno"`Error string `json:"error"`ErrorDescription string `json:"error_description"`Openid string `json:"openid"`SessionKey string `json:"session_key"`}/*** 測試demo*/func main() {ret, err := ReqSessionKey("8ba01454ac57775d3692f5dbfcac7a28NW", "myAppKey", "myAppSecret")fmt.Println(ret, err)}func ReqSessionKey(code, clientId, sk string) (*response, error) {data := make(url.Values)//由swan.getLoginCode獲取的臨時登錄憑證data.Add("code", code)//小程序appkeydata.Add("client_id", clientId)//小程序appSecretKeydata.Add("sk", sk)ret, err := netPost("https://spapi.baidu.com/oauth/jscode2sessionkey", &data)return ret, err}/*** http 請求方法*/func netPost(urlPath string, data *url.Values) (*response, error) {req, err := http.NewRequest("POST", urlPath, strings.NewReader(data.Encode()))req.Header.Add("content-type", "application/x-www-form-urlencoded")if err != nil {log.Println(err)return nil, err}client := &http.Client{Timeout: 5 * time.Second}resp, err := client.Do(req)if err != nil || resp.Body == nil {log.Println(err)return nil, err}defer resp.Body.Close()result, err := ioutil.ReadAll(resp.Body)if err != nil {log.Println(err)return nil, err}respData := &response{}err = json.Unmarshal(result, respData)if err != nil {log.Println(err)return nil, err}return respData, nil}
網(wǎng)站題目:創(chuàng)新互聯(lián)百度小程序教程:SessionKey
轉載來源:http://www.dlmjj.cn/article/dphcdsd.html


咨詢
建站咨詢
