doushaju4901
2016-12-28 08:34
浏览 136
已采纳

OAuth 2.0设置AccessToken

I am new with OAuth but because it looks very interesting, I wanted to take a look at it. I have read and viewed some tutorials and I understand the basics. I downloaded this BitBucket provider: https://github.com/stevenmaguire/oauth2-bitbucket which uses this client: https://github.com/thephpleague/oauth2-client

The thing I don't understand is: how do I set the accessToken? When there is no ?code in the url it redirects and adds it to the url, then I get the accessToken and the refreshToken but how do I use that accessToken? I've tried some things, also $provider->setAccessToken() but that function doesn't exist.

Hope someone can help me. It's probably pretty easy but I don't see it.

图片转代码服务由CSDN问答提供 功能建议

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

我不明白的是:如何设置accessToken? 如果url中没有?代码重定向并将其添加到url,那么我获取accessToken和refreshToken但是如何使用该accessToken? 我尝试了一些东西,也是$ provider-> setAccessToken(),但该函数不存在。

希望有人可以帮助我。 这可能很简单,但我没有看到它。

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

1条回答 默认 最新

  • dssu33392 2016-12-28 13:28
    已采纳

    The access token you received will not need to be set explicitly with any method. There are methods to get the user's details where this access token should be passed as parameter. As written in the documentation you provided:

    // 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...');
    

    Here getResourceOwner method should be given the token and the user details related to the $token will be returned.

    点赞 评论

相关推荐 更多相似问题