macellan / forge-client
Requires
- php: >=5.4
- ext-curl: *
- ext-json: *
- ext-mbstring: *
- autodesk/core: ^1.0
Requires (Dev)
- friendsofphp/php-cs-fixer: ~1.12
- phpunit/phpunit: ~4.8
- satooshi/php-coveralls: ~1.0
- squizlabs/php_codesniffer: ~2.6
- symfony/var-dumper: ^3.2
This package is auto-updated.
Last update: 2024-09-19 22:59:40 UTC
README
概述
此 PHP SDK 使您能够轻松地将 Forge REST API 集成到您的应用程序中,包括 OAuth、数据管理、模型衍生 和 设计自动化。
要求
- PHP 版本 5.4.0 及以上。
- 在 Forge 开发者门户 上注册的应用程序。
安装
Composer
要使用 Composer 安装绑定,请运行以下命令
composer require macellan/forge-client
手动安装
下载文件并包含 autoload.php
require_once('/path/to/ForgeClient/autoload.php');
教程
按照本教程查看一步一步的认证指南,以及如何使用 Forge API 的示例。
创建应用
在 Forge 开发者门户上创建一个应用。注意客户端 ID 和客户端密钥。
认证
此 SDK 包含一个 OAuth 2.0 客户端,允许您检索两腿和三腿令牌。它还允许您刷新三腿令牌。教程使用两腿和三腿令牌调用不同的数据管理端点。
两腿令牌
此类令牌直接提供给应用程序。
要获取两腿令牌,请运行以下代码。请注意,您需要用您的 应用 的客户端 ID 和客户端密钥替换 your-client-id
和 your-client-secret
。
<?php Autodesk\Auth\Configuration::getDefaultConfiguration() ->setClientId('<your-client-id>') ->setClientSecret('<your-client-secret>'); $twoLeggedAuth = new Autodesk\Auth\OAuth2\TwoLeggedAuth(); $twoLeggedAuth->setScopes(['bucket:read']); $twoLeggedAuth->fetchToken(); $tokenInfo = [ 'applicationToken' => $twoLeggedAuth->getAccessToken(), 'expiry' => time() + $twoLeggedAuth->getExpiresIn(), ];
三腿令牌
生成认证 URL
为了从用户那里请求权限以检索访问令牌,您需要将用户重定向到一个同意页面。
用您的 应用 的客户端 ID、客户端密钥和重定向 URL 替换 your-client-id
、your-client-secret
和 your-redirect-url
,并运行代码以创建一个同意页面 URL。
请注意,重定向 URL 必须与您在创建应用时提供的回调 URL 匹配。
<?php Autodesk\Auth\Configuration::getDefaultConfiguration() ->setClientId('<your-client-id>') ->setClientSecret('<your-client-secret>') ->setRedirectUrl('<your-redirect-url>'); $threeLeggedAuth = new Autodesk\Auth\OAuth2\ThreeLeggedAuth(); $threeLeggedAuth->addScope('code:all'); $authUrl = $threeLeggedAuth->createAuthUrl();
检索授权代码
一旦用户在同意页面上获得权限,Forge 将将页面重定向到您在创建应用时提供的重定向 URL。授权代码作为查询字符串返回。
GET /callback?code={authorizationCode}
检索访问令牌
使用您收到的授权代码请求访问令牌,如下所示
<?php Autodesk\Auth\Configuration::getDefaultConfiguration() ->setClientId('<your-client-id>') ->setClientSecret('<your-client-secret>') ->setRedirectUrl('<your-redirect-url>'); $threeLeggedAuth = new Autodesk\Auth\OAuth2\ThreeLeggedAuth(); $threeLeggedAuth->addScope('code:all'); $threeLeggedAuth->fetchToken($_GET['code']); $userToken = [ 'accessToken' => $threeLeggedAuth->getAccessToken(), 'refreshToken' => $threeLeggedAuth->getRefreshToken(), 'expiry' => time() + $threeLeggedAuth->getExpiresIn(), ];
请注意,访问令牌在短时间内会过期。字段 expires_in
表示访问令牌的有效期,单位为秒。要刷新访问令牌,请调用 $threeLeggedAuth->refreshToken($refreshToken);
方法。
示例API调用
使用 TwoLeggedAuth
对象或 ThreeLeggedAuth
对象调用 Forge API。
<?php $apiInstance = new Autodesk\Forge\Client\Api\ActivitiesApi($threeLeggedAuth); $activity = new \Autodesk\Forge\Client\Model\Activity(); // \Autodesk\Forge\Client\Model\Activity $result = $apiInstance->createActivity($activity);
API文档
您可以在开发者门户上找到API的完整文档
API端点文档
所有URI都是相对于https://developer.api.autodesk.com的。例如,createActivity URI是 'https://developer.api.autodesk.com/autocad.io/us-east/v2/Activities'。