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 远程桌面文档内容复制粘贴,格式会变化
    • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
    • ¥15 这种微信登录授权 谁可以做啊
    • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
    • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
    • ¥15 网络设备配置与管理这个该怎么弄
    • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
    • ¥20 西门子S7-Graph,S7-300,梯形图
    • ¥50 用易语言http 访问不了网页
    • ¥50 safari浏览器fetch提交数据后数据丢失问题