figachoco/forge-client

dev-master 2022-12-07 04:51 UTC

This package is auto-updated.

Last update: 2024-09-07 08:28:09 UTC


README

Forge API: oAuth2 Data-Management OSS Model-Derivative

概述

PHP SDK 使您能够轻松地将 Forge REST API 集成到您的应用程序中,包括 OAuth数据管理模型导出设计自动化

要求

安装

Composer

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

composer require autodesk/forge-client

手动安装

下载文件并包含autoload.php

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

教程

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

创建应用

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

认证

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

双端令牌

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

要获取双端令牌,请运行以下代码。注意需要用您的应用的客户端ID和客户端密钥替换your-client-idyour-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

要请求用户权限以获取访问令牌,您需要将用户重定向到同意页面。

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