oskmoz/forge-client

v1.1.7 2023-01-16 13:52 UTC

This package is auto-updated.

Last update: 2024-09-16 17:19:28 UTC


README

Forge API: oAuth2 Data-Management OSS Model-Derivative

概述

PHP SDK 允许您轻松将 Forge REST API 集成到您的应用程序中,包括 OAuth数据管理模型衍生设计自动化

要求

开始之前

  • 由于分支,文档并不全面
  • 从 forge-php-client 分支出来,以修复自 2022 年 9 月 30 日起的弃用问题,并保持此包正常工作

安装

Composer

要使用 Composer 安装绑定,请运行

composer require oskmoz/forge-php-client

手动安装

下载文件并包含 autoload.php

require_once('/path/to/ForgeClient/autoload.php');

教程

遵循此教程查看逐步认证指南,以及如何使用 Forge API 的示例。

创建应用

在 Forge 开发者门户上创建一个应用。注意客户端 ID 和客户端密钥。

认证

此 SDK 包含一个 OAuth 2.0 客户端,允许您检索双因素和三因素令牌。它还允许您刷新三因素令牌。教程使用双因素和三因素令牌调用不同的数据管理端点。

双因素令牌

此类型令牌直接提供给应用程序。

要获取双因素令牌,请运行以下代码。注意,您需要将 your-client-idyour-client-secret 替换为您应用的客户端 ID 和客户端密钥。

<?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

为了从用户那里获取权限以检索访问令牌,您将用户重定向到授权页面。

your-client-idyour-client-secretyour-redirect-url 替换为您应用的客户端 ID、客户端密钥和重定向 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'。

缩略图

thumbnail

支持

forge.help@autodesk.com

ISIBIM

库的新版本