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

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.

  • 写回答

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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码