通过code换取accesstoken
获取微信用户的授权, 需要使用微信支付提供的 OAuth2.0 服务.用授权小程序得到的授权码调用OAuth2.0接口access_token.
🔐
本接口服务已于 2021.11.10 (北京时间)停止开放,文档仅做留存参考。
| 请求参数 | 类型 | 描述 |
|---|---|---|
| query | object | 声明请求的查询参数 |
| mch_id | string | 商户号 |
| appid | string | 应用ID |
| openid | string | 微信openID |
| code | string | 第一步返回的auth_code |
| grant_type | string | 固定值authorization_codeauthorization_code 枚举值 |
| scope | string | 应用授权作用域pay_identity | pay_realname 枚举值之一 |
| sign_type | string | 签名类型HMAC-SHA256 枚举值 |
| sign | string | 签名 |
php
$instance->v2->appauth->getaccesstoken->getAsync([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
print_r(json_decode((string) $response->getBody()));
})
->wait();php
$instance->chain('v2/appauth/getaccesstoken')->getAsync([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
print_r(json_decode((string) $response->getBody()));
})
->wait();php
$instance['v2/appauth/getaccesstoken']->getAsync([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
])
->then(static function(\Psr\Http\Message\ResponseInterface $response) {
print_r(json_decode((string) $response->getBody()));
})
->wait();php
$response = $instance->v2->appauth->getaccesstoken->get([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
]);
print_r(json_decode((string) $response->getBody()));php
$response = $instance->chain('v2/appauth/getaccesstoken')->get([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
]);
print_r(json_decode((string) $response->getBody()));php
$response = $instance['v2/appauth/getaccesstoken']->get([
'query' => [
'mch_id' => '1200009811',
'appid' => 'wxcbda96de0b165486',
'openid' => 'baf04e6bbbd06f7b1a197d18ed53b7f1',
'code' => 'wxcbda96de0b165489',
'grant_type' => 'authorization_code',
'scope' => 'pay_identity',
'sign_type' => 'HMAC-SHA256',
'sign' => 'C380BEC2BFD727A4B6845133519F3AD6',
],
]);
print_r(json_decode((string) $response->getBody()));| 返回字典 | 类型 | 描述 |
|---|---|---|
| retcode | number | 返回状态码0 | -1 枚举值之一 |
| retmsg | string | 处理成功,返回ok |
| access_token | string | 接口调用凭证 |
| access_token_expire_in | integer | 请求返回的access_token过期时间,以秒为单位,有效期较短 |
| refresh_token | string | refresh令牌 |
| refresh_token_expire_in | number | refresh_token过期时间,以秒为单位,有效期较长 |