dongtiannai0654 2016-03-03 13:08
浏览 36

拆分OAuth php登录2个功能

I am using an OAuth plugin in my php application. All works well when I use all the code in one function. But I want split the OAuth call, and the OAuth token treatment. I try to create a global var for the provider object, but it doesn't work. I always have an error with the $provider object. This is the function I have for the moment :

function login(){
        $provider = new Stevenmaguire\OAuth2\Client\Provider\Microsoft([
        'clientId'          => 'myclientid',
        'clientSecret'      => 'mysecret',
        'redirectUri'       => 'https://mywebsite'
        //'redirectUri'       => 'https://mywebsite/loginMicrosoft' //The url where I want use the token
    ]);

    if (!isset($_GET['code'])) {

        $authUrl = $provider->getAuthorizationUrl();
        $this->Session->write('oauth2state', $provider->getState());

    // This is the part I want put in the function loginMicrosoft. All else if and else
    } elseif (empty($_GET['state']) || ($_GET['state'] !== $this->Session->read('oauth2state'))) {

        $this->Session->destroy();
        exit('Invalid state');

    } else {
        // Try to get an access token (using the authorization code grant)
        $token = $provider->getAccessToken('authorization_code', [
            'code' => $_GET['code']
        ]);

        // Use this to interact with an API on the users behalf
        $this->log( $token->getToken());
    }
}
//This is the function where I want put the elseif and else treatment
function loginMicrosoft (){
}

Thanks for your help.

EDIT : I also try to put my provider object in session, but I have the message "Call to a member function getAccessToken() on a non object"

EDIT 2 : After unsuccessfully tried, I keep all the OAuth code in the same function

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
    • ¥15 Vue3地图和异步函数使用
    • ¥15 C++ yoloV5改写遇到的问题
    • ¥20 win11修改中文用户名路径
    • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
    • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
    • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
    • ¥15 帮我写一个c++工程
    • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
    • ¥15 关于smbclient 库的使用