【问题标题】:OAuth 2.0 Set AccessTokenOAuth 2.0 设置访问令牌
【发布时间】:2016-12-28 08:34:34
【问题描述】:

我是 OAuth 新手,但因为它看起来很有趣,所以我想看看它。我已阅读并查看了一些教程,并且了解了基础知识。我下载了这个 BitBucket 提供程序:https://github.com/stevenmaguire/oauth2-bitbucket,它使用了这个客户端:https://github.com/thephpleague/oauth2-client

我不明白的是:如何设置accessToken?当 url 中没有 ?code 时,它​​会重定向并将其添加到 url,然后我得到 accessToken 和 refreshToken 但如何使用该 accessToken?我已经尝试了一些东西,还有 $provider->setAccessToken() 但该功能不存在。

希望有人可以帮助我。这可能很容易,但我看不到。

【问题讨论】:

    标签: php oauth-2.0


    【解决方案1】:

    您收到的访问令牌不需要使用任何方法显式设置。有一些方法可以获取用户的详细信息,此访问令牌应作为参数传递。正如您提供的documentation 中所写:

    // Try to get an access token (using the authorization code grant)
    $token = $provider->getAccessToken('authorization_code', [
        'code' => $_GET['code']
    ]);
    
    // Optional: Now you have a token you can look up a users profile data
    try {
    
        // We got an access token, let's now get the user's details
        $user = $provider->getResourceOwner($token);
    
        // Use these details to create a new profile
        printf('Hello %s!', $user->getId());
    
    } catch (Exception $e) {
    
        // Failed to get user details
        exit('Oh dear...');
    

    这里应该给getResourceOwner方法提供token,并返回与$token相关的用户详细信息。

    【讨论】: