barretzhi/socialite

从 laravel/socialite 提取的 OAuth 2 包的集合。

1.3.0 2017-08-04 06:28 UTC

README

Build Status Latest Stable Version Latest Unstable Version Build Status Scrutinizer Code Quality Code Coverage Total Downloads License

Socialite 是一个 OAuth2 身份验证工具。它受到 laravel/socialite 的启发,您可以在任何 PHP 项目中轻松使用它。


 创造不息,交付不止

要求

PHP >= 5.4

安装

$ composer require "overtrue/socialite:~1.1"

使用

对于 Laravel 5: overtrue/laravel-socialite

authorize.php:

<?php
use Overtrue\Socialite\SocialiteManager;

$config = [
    'github' => [
        'client_id'     => 'your-app-id',
        'client_secret' => 'your-app-secret',
        'redirect'      => 'https:///socialite/callback.php',
    ],
];

$socialite = new SocialiteManager($config);

$response = $socialite->driver('github')->redirect();

echo $response;// or $response->send();

callback.php:

<?php

// ...
$user = $socialite->driver('github')->user();

$user->getId();        // 1472352
$user->getNickname();  // "overtrue"
$user->getName();      // "安正超"
$user->getEmail();     // "anzhengchao@gmail.com"
$user->getProviderName(); // GitHub
...

配置

现在我们支持以下站点

facebookgithubgooglelinkedinweiboqqwechatwechat_opendouban

每个驱动都使用相同的配置键:client_idclient_secretredirect

示例

...
  'weibo' => [
    'client_id'     => 'your-app-id',
    'client_secret' => 'your-app-secret',
    'redirect'      => 'https:///socialite/callback.php',
  ],
...

针对 微信开放平台 的特殊配置选项

'wechat_open' => [
    'client_id'     => 'your-app-id',
    'client_secret' => ['your-component-appid', 'your-component-access-token'],
    'redirect'      => 'https:///socialite/callback.php',
]

范围

在重定向用户之前,您还可以使用 scope 方法在请求上设置 "scopes"。此方法将覆盖所有现有范围

$response = $socialite->driver('github')
                ->scopes(['scope1', 'scope2'])->redirect();

重定向 URL

您还可以动态设置 redirect,您可以使用以下方法更改 redirect URL

$socialite->redirect($url);
// or
$socialite->withRedirectUrl($url)->redirect();
// or
$socialite->setRedirectUrl($url)->redirect();

微信范围

  • snsapi_basesnsapi_userinfo - 用于媒体平台认证。
  • snsapi_login - 用于网页认证。

附加参数

要包含请求中的任何可选参数,请使用关联数组调用 with 方法

$response = $socialite->driver('google')
                    ->with(['hd' => 'example.com'])->redirect();

用户界面

标准用户 API

$user = $socialite->driver('weibo')->user();
{
  "id": 1472352,
  "nickname": "overtrue",
  "name": "安正超",
  "email": "anzhengchao@gmail.com",
  "avatar": "https://avatars.githubusercontent.com/u/1472352?v=3",
  "original": {
    "login": "overtrue",
    "id": 1472352,
    "avatar_url": "https://avatars.githubusercontent.com/u/1472352?v=3",
    "gravatar_id": "",
    "url": "https://api.github.com/users/overtrue",
    "html_url": "https://github.com/overtrue",
    ...
  },
  "token": {
    "access_token": "5b1dc56d64fffbd052359f032716cc4e0a1cb9a0",
    "token_type": "bearer",
    "scope": "user:email"
  }
}

您可以根据如下方式获取用户属性作为数组键

$user['id'];        // 1472352
$user['nickname'];  // "overtrue"
$user['name'];      // "安正超"
$user['email'];     // "anzhengchao@gmail.com"
...

或使用方法

$user->getId();
$user->getNickname();
$user->getName();
$user->getEmail();
$user->getAvatar();
$user->getOriginal();
$user->getToken();// or $user->getAccessToken()
$user->getProviderName(); // GitHub/Google/Facebook...

获取 OAuth API 的原始响应

方法 $user->getOriginal() 将返回 API 原始响应的数组。

获取访问令牌对象

您可以通过调用 $user->getToken()、$user->getAccessToken() 或 $user['token'] 来获取当前会话的访问令牌实例。

使用访问令牌获取用户

$accessToken = new AccessToken(['access_token' => $accessToken]);
$user = $socialite->user($accessToken);

自定义 Session 或 Request 实例。

在调用 driver 方法之前,您可以将请求设置为您的自定义 Request 实例,该实例 instanceof Symfony\Component\HttpFoundation\Request

$request = new Request(); // or use AnotherCustomRequest.

$socialite = new SocialiteManager($config, $request);

或设置请求到 SocialiteManager 实例

$socialite->setRequest($request);

您可以通过 getRequest()SocialiteManager 实例中获取请求

$request = $socialite->getRequest();

设置自定义会话管理器。

默认情况下,SocialiteManager 使用 Symfony\Component\HttpFoundation\Session\Session 实例作为会话管理器,您可以根据以下代码更改它

$session = new YourCustomSessionManager();
$socialite->getRequest()->setSession($session);

您的自定义会话管理器必须实现 Symfony\Component\HttpFoundation\Session\SessionInterface

享受它! ❤️

参考

许可证

MIT