我有以下设置来获取访问令牌:
import * as docusign from 'docusign-esign';
const expiresIn = 28800;
export const createClient = async (): Promise<docusign.ApiClient> => {
const apiClient = new docusign.ApiClient({ basePath: process.env.BASE_PATH, oAuthBasePath: process.env.O_AUTH_BASE_PATH });
try {
const response = await apiClient.requestJWTApplicationToken(
process.env.CLIENT_ID,
["signature", "impersonation"],
Buffer.from(process.env.RSA_PRIVATE_KEY.replace(/\\n/g, '\n')),
expiresIn
);
console.log(response.body.access_token) // -> successfully returns access token
apiClient.addDefaultHeader("Authorization", "Bearer " + response.body.access_token);
} catch (error){ throw AppError("My custom error") }
return apiClient;
}
然后在另一个地方我有以下函数应该创建一个信封:
import { EnvelopeDefinition, EnvelopeRecipients, EnvelopesApi, Signer } from 'docusign-esign';
import { createClient } from "./above-file-path.ts";
export const someFunction = async () => {
const signer1: Signer = { email: '[email protected]', name: 'my name', roleName: 'my name' };
const signer2: Signer = { email: '[email protected]', name: 'client name', roleName: 'Client' };
const recipients: EnvelopeRecipients = { signers: [signer1, signer2] };
const dsApiClient = await createClient();
const envelope: EnvelopeDefinition = {
emailSubject: 'Please sign this agreement',
templateId: 'uuid-of-my-template',
status: 'created',
recipients,
};
const envelopesApi = new EnvelopesApi(dsApiClient);
try {
const { envelopeId, errorDetails } = await envelopesApi.createEnvelope(process.env.ACCOUNT_ID, {
envelopeDefinition: envelope,
});
console.log("Success?: ", envelopeId)
} catch (error) {
console.log(error) // -> This gives: Unauthorized...{"errorCode":"AUTHORIZATION_INVALID_TOKEN","message":"The access token provided is expired, revoked or malformed. Authentication for System Application failed."}
}
}
我不断收到
AUTHORIZATION_INVALID_TOKEN
错误。
补充说明:
account-d.docusign.com
代表 O_AUTH_BASE_PATH
,使用 https://demo.docusign.net/restapi
代表 BASE_PATH
。我花了一点时间,但我明白发生了什么。您正在获取仅适用于某些管理 API 端点的 JWT 应用程序令牌。您需要的是 requestJwtUserToken() 函数。请查看此页面了解用户令牌方法的工作原理。它本质上只需要一个参数,即被模拟用户的 userId。尝试一下。