dan-rogers/oauth-php

用于OAuth 1和OAuth 2工作流程的库。

v1.0.2 2023-04-19 14:27 UTC

This package is auto-updated.

Last update: 2024-09-19 17:31:20 UTC


README

A PHP Library for OAuth 1 and OAuth 2 workflows.

安装

composer require dan-rogers/oauth-php

使用

OAuth 1 示例

  • 使用选项数组创建认证生成的配置。
  • 构建一个新的 OAuth1Config 对象,并将数组作为参数。
  • 构建一个新的 OAuth1 对象,并将您的 OAuth1Config 对象作为参数。
  • 调用 generateAuthorization()。

只要您提供了OAuth 1步骤的正确信息,它将生成一个有效的认证头。

有关OAuth 1使用的更多信息,请参阅

#!/usr/bin/php
<?php

use OAuth\OAuth1\OAuth1;
use OAuth\OAuth1\OAuth1Config;

require __DIR__ . '/vendor/autoload.php';

$config  = new OAuth1Config([
    'oauth_callback' => 'https://my_website/my_service/auth',
    'oauth_consumer_key' => 'CONSUMER_KEY',
    'consumer_secret' => 'CONSUMER_SECRET',
    'realm' => 'MY_REALM',
    'oauth_signature_method' => OAuth1Config::HMAC_SHA256,
]);

$oauth = new OAuth1($config);
$auth_header = $oauth->generateAuthorization('https://third_party/token_endpoint', 'POST');

// Example usage using PHP CURL

$ch = curl_init();
curl_setopt_array($ch, [
    CURLOPT_URL => 'https://third_party/token_endpoint',
    CURLOPT_CUSTOMREQUEST => 'POST',
    CURLOPT_HTTPHEADER => [
        'Authorization: ' . $auth_header,
        'Content-Length: 0',
    ],
]);

$response = curl_exec($ch);