douliao1911 2015-10-07 05:36
浏览 53

使用Laravel的Battlenet Oauth2 - 无法获得正确的输出

I've tried both of these libraries for the Oauth 2.0 process:

1) https://github.com/thephpleague/oauth2-client 2) https://github.com/tpavlek/oauth2-bnet

But I haven't really made either of them work. I'm fairly new to Laravel but I want to use this framework for this project.

Here's the code I've been working on:

Routes

Route::get('oAuth/authRequest', 'CommunityOAuthProfile@authRequest');

Route::get('oAuth/authCode', 'CommunityOAuthProfile@authCode');

Controllers(CommunityOAuthProfile.php)

<?php

namespace App\Http\Controllers;

use App\User;
use App\Http\Controllers\Controller;
use OAuth;

class CommunityOAuthProfile extends Controller
{
public function authRequest()
{
    $provider = new \Depotwarehouse\OAuth2\Client\Provider\WowProvider([
        'clientId'                => <client_id>,    // The client ID assigned to you by the provider
        'clientSecret'            => <client_secret>,   // The client password assigned to you by the provider
        'redirectUri'             => 'https://sc2data.com/oAuth/authCode',
    ]);

    if (!isset($_GET['code'])) 
    {
        $authorizationUrl = $provider->getAuthorizationUrl();
        $_SESSION['oauth2state'] = $provider->getState();

        header('Location: ' . $authorizationUrl);
        exit;
    }
    elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {

        unset($_SESSION['oauth2state']);
        exit('Invalid state');

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

            $user = $this->provider->getResourceOwner($token);

            // We have an access token, which we may use in authenticated
            // requests against the service provider's API.
            // echo $accessToken->getToken() . "
";
            // echo $accessToken->getRefreshToken() . "
";
            // echo $accessToken->getExpires() . "
";
            // echo ($accessToken->hasExpired() ? 'expired' : 'not expired') . "
";

            // // Using the access token, we may look up details about the
            // // resource owner.
            // $resourceOwner = $provider->getResourceOwner($accessToken);

            // var_export($resourceOwner->toArray());

            // // The provider provides a way to get an authenticated API request for
            // // the service, using the access token; it returns an object conforming
            // // to Psr\Http\Message\RequestInterface.
            // $request = $provider->getAuthenticatedRequest(
                // 'GET',
                // 'http://brentertainment.com/oauth2/lockdin/resource',
                // $accessToken
            // );

        }
        catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) 
        {
            exit($e->getMessage());
        }
    }
}

public function authCode()
{   
    $provider = new \Depotwarehouse\OAuth2\Client\Provider\WowProvider([
        'clientId'                => <client_id>,    // The client ID assigned to you by the provider
        'clientSecret'            => <client_secret>,   // The client password assigned to you by the provider
        'redirectUri'             => 'https://sc2data.com/oAuth/authCode',
    ]);

    try
    {
        $accessToken = $provider->getAccessToken('authorization_code', [
                    'code' => $_GET['code']
                ]);

        $user = $this->provider->getResourceOwner($token);
    }
    catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) 
    {
        exit($e->getMessage());
    }
}

}

By the way, is there any recommended oauth2 library for php? So far, these 2 are the most promising I've seen but there may be others I'm not aware of?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
    • ¥20 关于URL获取的参数,无法执行二选一查询
    • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
    • ¥15 marlin编译错误,如何解决?
    • ¥15 有偿四位数,节约算法和扫描算法
    • ¥15 VUE项目怎么运行,系统打不开
    • ¥50 pointpillars等目标检测算法怎么融合注意力机制
    • ¥20 Vs code Mac系统 PHP Debug调试环境配置
    • ¥60 大一项目课,微信小程序
    • ¥15 求视频摘要youtube和ovp数据集