Skip to content

通过code换取accesstoken

获取微信用户的授权, 需要使用微信支付提供的 OAuth2.0 服务.用授权小程序得到的授权码调用OAuth2.0接口access_token.

🔐

本接口服务已于 2021.11.10 (北京时间)停止开放,文档仅做留存参考。

请求参数类型描述
queryobject声明请求的查询参数
mch_idstring商户号
appidstring应用ID
openidstring微信openID
codestring第一步返回的auth_code
grant_typestring固定值authorization_code
authorization_code 枚举值
scopestring应用授权作用域
pay_identity | pay_realname 枚举值之一
sign_typestring签名类型
HMAC-SHA256 枚举值
signstring签名
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()));
返回字典类型描述
retcodenumber返回状态码
0 | -1 枚举值之一
retmsgstring处理成功,返回ok
access_tokenstring接口调用凭证
access_token_expire_ininteger请求返回的access_token过期时间,以秒为单位,有效期较短
refresh_tokenstringrefresh令牌
refresh_token_expire_innumberrefresh_token过期时间,以秒为单位,有效期较长

参阅 官方文档

Published on the GitHub by TheNorthMemory