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.

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

报告相同问题?

悬赏问题

  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥30 求一段fortran代码用IVF编译运行的结果
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛