stccorp/yahoo-oauth2

此包的最新版本(dev-main)没有提供许可证信息。

使用 OAuth2 验证 Yahoo 用户的库

dev-main 2022-09-12 20:41 UTC

This package is not auto-updated.

Last update: 2024-09-23 22:26:19 UTC


README

创建一个 yconfig.php

require_once 'vendor/autoload.php';

use stccorp\YahooAuth\YahooClient;

const YAHOO_CLIENT_ID = 'Your client Id';
const YAHOO_CLIENT_SECRET = 'Your Secret';
const YAHOO_REDIRECT_URI =  'Your Redirect Url';

$_SESSION['nonce'] = bin2hex(random_bytes(128/8));//so I can check the input later

$yahoo_client = new YahooClient();
$yahoo_client->setClientId(YAHOO_CLIENT_ID);
$yahoo_client->setClientSecret(YAHOO_CLIENT_SECRET);
$yahoo_client->setRedirectUri(YAHOO_REDIRECT_URI);
$yahoo_client->addScope('openid');
$yahoo_client->setNonce($_SESSION['nonce']);

你将有一个包含 Yahoo 登录按钮的页面,比如说 index.php

require_once 'yconfig.php';

$yahoo_login_btn = '<a alt="Sign in with Yahoo" href="' . filter_var($yahoo_client->createAuthUrl(), FILTER_SANITIZE_URL) . '"><img src="btn_yahoo_rectangle.png" width="200"/></a>';

然后你需要一个 yahoo_callback.php

require_once 'yconfig.php';

if (strlen($_GET['code'] ?? '') > 0) {
  $token = $yahoo_client->fetchAccessTokenWithAuthCode($code);
  
   if (!$token->hasData()) {
      die("No Data");
   }
   
   $yahoo_client->setTokens($token);
   
   $data_json = $yahoo_client->getUserInfo();
   $data = json_decode($data_json);
}