w7corp / easywechat

📦 一个 PHP 微信 SDK
https://easywechat.com
MIT License
10.26k stars 2.41k forks source link

拿到code=CODE&state=STATE之后怎么拿到openid? #452

Closed sidgwick closed 8 years ago

sidgwick commented 8 years ago

我用的环境

LNMP

PHP 版本:7.0.4 overtrue/wechat 版本:3.1.0

问题及现象

image

这里我拿到了codestate, 之后是怎么取到用户信息的? 按照文档, 在callback.php直接用$auth->user()会报The key "access_token" could not be empty.

我得callback模仿这里写的: image

我是不是少了一步?

sidgwick commented 8 years ago

额, 我懂了. $app->auth->user()的app的配置不能带oauth的配置?

原来的app, 这个取不到用户信息:

$this->app = new Application([
    'app_id' => self::APPID,
    'secret'  => self::APPSECRET,
    'debug'  => true,
    'oauth' => [
        'scopes'   => ['snsapi_userinfo'],
        'callback' => '/auth/weixin-openid',
    ],
]);

$this->app->oauth->user();

后来的app, 可以取到:

(new Application([
    'app_id' => self::APPID,
    'secret'  => self::APPSECRET,
    'debug'  => true,
]))->oauth->user();