imper86/php-asana-api

简单而酷的PHP Asana API客户端

v2.0.4 2020-05-14 19:56 UTC

This package is auto-updated.

Last update: 2024-09-15 05:34:17 UTC


README

安装

只需使用composer

composer require imper86/php-asana-api

身份验证

库中包含许多机制,允许您忘记令牌、过期等。但为了开始使用它,您必须通过OAuth流程授权用户。

创建Credentials对象

$credentials = new Credentials(
    'your_client_id',
    'your_client_secret',
    'https://:8000/asana' //redirect_uri
);

创建TokenRepository对象

$tokenRepository = new FileTokenRepository(__DIR__ . '/asana_tokens');

您可以创建自己的TokenRepository,只需实现TokenRepositoryInterface。您可以使用数据库、Redis或任何您想要的东西。

创建客户端

$client = new Client($credentials, $tokenRepository);

获取授权URL,并将用户重定向

$state = 'your-random-secret-state';
header(sprintf('Location: %s', $client->auth()->authUri($state)));

在成功授权后,用户将带有statecode参数的重定向到您的redirect_uri

验证state并验证您的客户端

if ($state === $_GET['state'] ?? null) {
    throw new Exception('CSRF?!');
}

$client->authenticateWithCode($_GET['code']);

库将使用您的TokenRepository来存储令牌,从现在起,您只需关心存储用户的Asana gid。

您可以使用以下方式获取该ID

$client->getUserGid();

用法

就像在身份验证部分一样,创建客户端

$userGid = 'your-user-gid';
$client = new Client($credentials, $tokenRepository, $userGid);

从现在起,您可以在$client上使用这些方法

$client->organizations()->(...)
$client->projects()->(...)
$client->sections()->(...)
$client->tags()->(...)
$client->tasks()->(...)
$client->teams()->(...)
$client->users()->(...)
$client->workspaces()->(...)

快速示例

$projects = $client->teams()->projects()->list('teamgid');

CRUD操作命名

  • list() - 获取集合
  • show() - 获取项目
  • remove() - 删除项目
  • update() - 更新项目

如果您使用像PHPStorm这样的带有类型提示的IDE,您将很容易理解。如果不这样,请查看Resource目录

贡献

任何帮助都将非常感激。

这个库还没有完成,并不是所有的资源都被覆盖。x