antevenio / mdirector-oauth-client
用于访问mdirector API服务的特定OAuth客户端库
Requires
- ext-json: *
- league/oauth2-client: ^2.3
- symfony/console: *
Requires (Dev)
- antevenio/md-phpqa: ^0.1.0
- mockery/mockery: ^1.2
- php-coveralls/php-coveralls: ^2.1
- phpunit/phpunit: ^5.7
README
特定于访问MDirector API服务的OAuth客户端库,用PHP编写。
此包提供MDirector (http://www.mdirector.com) OAuth 2.0支持PHP League的OAuth 2.0客户端。
目前,仅提供MDirector电子邮件营销和事务应用的OAuth2实现。此包由两个oauth2-client提供程序(电子邮件营销和事务服务)以及围绕它们的外壳组成,以隐藏所需OAuth2协商的负担。
作为消费者,您可以选择使用提供程序或客户端包装,具体取决于您最合适。
还有一个命令行脚本,可以帮助您从shell测试它。
需求
支持以下版本的PHP。
- PHP 5.6
- PHP 7.0
- PHP 7.1
- PHP 7.2
安装
composer require antevenio/oauth2-mdirector
使用
如前所述,您可以选择仅使用提供程序或其包装。在这里,您可以找到每个案例的示例
1. MDirector Oauth2-client提供程序(电子邮件营销应用)
您可以在oauth2-client提供程序下找到OAuth2/Client/Provider,对于通用使用说明,请参阅oauth2-client github项目中的通用使用说明。
目前,MDirector电子邮件营销提供程序仅提供名为webapp的通用clientId的资源所有者密码凭据授予。以下是一个获取有效accessToken的示例
$provider = new \MDOAuth\OAuth2\Client\Provider\MDirector(); try { // Try to get an access token using the resource owner password credentials grant. $accessToken = $provider->getAccessToken('password', [ 'username' => '{yourCompanyId}', 'password' => '{yourApiSecret}' ]); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token exit($e->getMessage()); }
在构建对mdirector API的请求时,请考虑我们的API期望参数在GET请求的查询字符串中,或在请求主体的application/x-www-form-urlencoded中,对于任何其他方法
例如,POST,PUT,DELETE等。
2. 事务性Oauth2-client提供程序(事务应用)
您可以在oauth2-client提供程序下找到OAuth2/Client/Provider,对于通用使用说明,请参阅oauth2-client github项目中的通用使用说明。
目前,事务性提供程序仅提供名为webapp的通用clientId的资源所有者密码凭据授予。以下是一个获取有效accessToken的示例
$provider = new \MDOAuth\OAuth2\Client\Provider\Transactional(); try { // Try to get an access token using the resource owner password credentials grant. $accessToken = $provider->getAccessToken('password', [ 'username' => '{yourCompanyId}', 'password' => '{yourApiSecret}' ]); } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) { // Failed to get the access token exit($e->getMessage()); }
在构建对事务API的请求时,请考虑我们的API期望参数在GET请求的查询字符串中,或在请求主体的application/json编码中,对于任何其他方法
例如,POST,PUT,DELETE等。
以下是必需的头部信息
Content-Type: application/json
和
Accept: application/json
在每个请求中都是必需的。
3. 包装客户端
包装客户端提供了一种简化的方式来调用API。它们负责获取令牌并在需要时刷新它们。您只需将参数作为关联数组设置,包装器知道如何根据指定的方法传递它们。您还可以指定一个自定义用户代理,在请求头中发送("oauth2-mdirector client"为默认值)
使用示例
$companyId = 'yourCompanyId'; $secret = 'yourApiSecret'; $client = (new \MDOAuth\OAuth2\Wrapper\MDirector\Factory())->create($companyId, $secret); $response = $client->setUri('https://api.mdirector.com/api_contact') ->setMethod('get') ->setParameters([ 'email' => 'myemail@mydomain.org' ]) ->setUserAgent('MyOwnUserAgent 1.0') ->request(); echo $response->getBody()->getContents();
3. Shell脚本
此库还提供了一个console客户端,您可以从中调用mdirector API。要这样做,请运行
$ ./bin/mdirector-oauth-client oauth2:mdirector --help
该命令将显示有关其用法和参数的一些自我解释的帮助信息。
4. 其他
如果您打算使用其他客户端实现,或者必须使用除PHP之外的语言调用API,这里您可以找到需要了解的一些基本信息。
您将使用"webapp"作为固定的clientId,指定password作为授权类型,您的公司id作为username,您的secret作为密码来请求访问令牌。
获取此类令牌(或刷新它们)的端点为
https://app.mdirector.com/oauth2
您将在请求中携带Bearer头部的令牌。(https://oauth.ac.cn/2/bearer-tokens/)