智能小程序在其服务端中发送 POST 请求到百度 oauth2.0 授权服务地址,并带上对应的参数,便可获取到 Session Key 。

获取 Session Key 的 URL 地址 :
https://spapi.baidu.com/oauth/jscode2sessionkey
为了让您的智能小程序运行在联盟 App 上,我们对获取 SessionKey 的接口做了升级。新接口可以兼容获取百度内部 App 和百度外部 App 的 SessionKey。
您只需要将原接口地址
https://openapi.baidu.com/nalogin/getSessionKeyByCode
更改为
https://spapi.baidu.com/oauth/jscode2sessionkey
即可让您的智能小程序在其它 App 上自动实现账号关联。
| 参数名 | 类型 | 是否必须 | 描述 |
|---|---|---|---|
| Content-Type | Application/x-www-form-urlencoded | 是 | HTTP 的实体首部字段,浏览器原生 form 表单 |
| 参数名 | 是否必须 | 说明 |
|---|---|---|
| code | 是 | 通过 swan.getLoginCode 获取 Authorization Code 特殊说明:code 中有@符号时,会请求对应的开源宿主,用户身份校验及 SessionKey 生成过程由开源宿主实现 |
| client_id | 是 | 智能小程序的 AppKey 智能小程序 AppKey 示例:4fecoAqgCIUtzIyA4FAPgoyrc4oUc25c |
| sk | 是 | 智能小程序的 AppSecret |
| 字段名 | 说明 |
|---|---|
| openid | 用户身份标识,由 appid 和 uid 生成 不同用户登录同一个小程序获取到的 openid 不同,同一个用户登录不同小程序获取到的 openid 也不同 |
| session_key | 用户的 Session Key |
{openid: "l214zFqNrEuIEnp6m7Y01sw8yj",session_key: "981ce8b151c0599acf7ad1a673c6ff5e"}
| 字段名 | 说明 |
|---|---|
| errno | 错误码,详情见下方错误码 |
| error | 错误描述 |
| error_description | 错误描述信息,用来帮助理解和解决发生的错误 |
{"errno": 10010100,"error": "parameter is invalid","error_description": "Key: 'Code2SessionKeyParam.ClientID' Error:Field validation for 'ClientID' failed on the 'required' tag"}
| 错误码 | 描述 | 错误原因自查 |
|---|---|---|
| 10010100 | 参数错误或 code 的值无效 | 1 请检查使用 client_id 参数是否是 appKey 2 请检查生成 code 和使用 code 的时间差是否超过了有效期,有效期为 10s 3 请检查生成 code 的小程序的 appKey 和当前请求接口的 client_id 是否一致 |
| 10010400 | client_id 与 sk 不匹配 | 1 请联系小程序管理员检查小程序 appSecret 是否有修改 |
| 10010300 | 请求开源宿主失败 | 1 信息为开源宿主返回,请参考 error_description 描述进行错误自查 |
/*** 获取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* 传递的数组参数* @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")//返回数据结构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}