php 为什么我在尝试调用AdobeSign API时收到错误400 Bad Request“INVALID_X_API_USER_HEADER”?

wdebmtf2  于 2023-04-28  发布在  PHP
关注(0)|答案(1)|浏览(159)

简介:目前,我正在开发一个应用程序(在laravel-backpack中),您可以在下一张图片中查看处方集:formulary_of_my_app

我尝试使用Adobe Sign的API(此应用程序的目的是简化签署合同的过程,只需添加PDF并单击“Firmar”按钮(Firmar = Sign)即可自动完成,这将发送PDF)。我有我需要的一切(开发人员帐户,IntegrationKey,SecretKey,UserId,Access-Token等)。..)来调用API。

我得到的错误:但我有这个问题,它说,“API-USER-HEADER”是无效的,因为提供的值是无效的格式。在下一个图像中,您可以检查错误:error_invalid_x_api_user
Php Code(下面是我用来调用API的代码):

<?php

namespace App\Services;

use GuzzleHttp\Client;

class AdobeSign
{
    private $client;
    private $baseUrl;
    private $integrationKey;
    private $secretKey;
    private $userId;

    public function __construct()
    {
        $this->client = new Client();
        $this->baseUrl = "https://api.eu1.echosign.com/api/rest/v6";/*URL DE LA API*/
        $this->integrationKey = env('ADOBE_SIGN_INTEGRATION_KEY');/*De la cuenta de Adobe Sign*/
        $this->secretKey = env('ADOBE_SIGN_SECRET_KEY');/*De la cuenta de Adobe Sign*/
        $this->userId = env('ADOBE_SIGN_USER_ID');/*De la cuenta de Adobe Sign*/
    }

    private function getAuthorizationHeader()
    { 
        $headers = [
            'Content-Type' => 'application/json',
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
            'Authorization' => sprintf('Bearer %s', env('ADOBE_SIGN_TOKEN')),
            'x-api-user' => base64_encode(json_encode([
                'email' => 'email@domain.domain',
                'firstName' => 'name',
                'lastName' => 'secondName',
                'groupIds' => [env('ADOBE_SIGN_GROUP_ID')],
                'userId' => env('ADOBE_SIGN_USER_ID')
            ])),
        ];

        $timeStamp = time();
        //$headers['x-api-user'] = sprintf('{"userId":"%s"}', $this->userId);
        $headers['x-sdk-date'] = gmdate('D, d M Y H:i:s T', $timeStamp);
        
        print_r($headers);
        echo(base64_encode(json_encode($headers)));
        //$headers['x-api-user'] = base64_encode(json_encode($headers));
        return $headers;
    }

    private function getAccessToken($timeStamp)
    {
        $jwtPayload = [
            'iss' => $this->integrationKey,
            'sub' => base64_encode(json_encode(env('ADOBE_SIGN_USER_EMAIL'))),
            'aud' => 'https://api.eu1.echosign.com',
            'exp' => $timeStamp + 3600,
            'iat' => $timeStamp
        ];

        $jwtHeader = [
            'alg' => 'HS256',
            'typ' => 'JWT'
        ];

        $jwtHeaderEncoded = base64_encode(json_encode($jwtHeader));
        $jwtPayloadEncoded = base64_encode(json_encode($jwtPayload));
        $jwtSignatureEncoded = base64_encode(hash_hmac('sha256', "$jwtHeaderEncoded.$jwtPayloadEncoded", $this->secretKey, true));

        $jwtToken = "$jwtHeaderEncoded.$jwtPayloadEncoded.$jwtSignatureEncoded";

        return $jwtToken;
    }

    public function sendAgreement($name, $email, $fileUrl, $X, $Y, $page)
    {
        $response = $this->client->post($this->baseUrl . '/agreements', [
            'headers' => $this->getAuthorizationHeader(),
            'json' => [
                'documentCreationInfo' => [
                    'fileInfos' => [/*Documento pdf INFO*/
                        [
                            'libraryDocumentId' => '',/*Se utiliza si ya esta en la biblioteca*/
                            'transientDocumentId' => '',/*Se utiliza si el archivo aún no se ha subido a la biblioteca*/
                            'documentURL' => $fileUrl,/*URL DEL ARCHIVO*/
                            'name' => $name/*nombre del archivo*/
                        ]
                    ],
                    'recipientSetInfos' => [/*Destinatario INFO*/
                        [
                            'recipientSetMemberInfos' => [/*Info, por si se quieren añadir mas de un destinatario */
                                [
                                    'email' => $email/*Email del destinatario*/
                                ]
                            ],
                            'recipientSetRole' => 'SIGNER'/*ROL DEL DESTINATARIO [SIGNER :=: Firmar el acuerdo]*/
                        ]
                    ],
                    'signatureType' => 'ESIGN', /*TIPO DE FIRMA QUE SE UTILIZARÁ [ESIGN :=: Puede firmar el acuerdo digitalmente]*/
                    'signatureFlow' => 'SENDER_SIGNATURE_NOT_REQUIRED',/*FLUJO DE LA FIRMA [SENDER_SIGNATURE_NOT_REQUIRED :=: El que lo envia necesita firmar el acuerdo]*/
                    'name' => $name,/*NOMBRE DEL ACUERDO QUE SE VA A FIRMAR */
                    'signatureFieldInfos' => [
                        [
                            'defaultValue' => '',
                            'locations' => [
                                [
                                    'height' => $Y,
                                    'left' => $X,
                                    'pageNumber' => $page,
                                    'top' => '50',
                                    'width' => '100'
                                ]
                            ],
                            'name' => 'Signature'
                        ]
                    ]
                ]
            ]
        ]);
    }
}

我已经尝试过什么?:我浏览了一堆与此错误相关的网站,并查看了Adobe文档,但我仍然被卡住了。此外,我尝试更改代码(它没有产生影响),并测试它是否真的响应了此AdobeSign链接中的API。当我在我提供的AdobeSign链接中进行测试时,我得到了以下结果:test_adobe_sign_1test_adobe_sign_2"

它真的工作,因为你可以看到在图像中,因为是回馈“响应体”,但在我的代码中的东西是失踪或我做错了什么。

eh57zj3b

eh57zj3b1#

UPDATE“解决方案”:我不太理解Adobe Sign文档。主要的问题是我写的是字面上的userid:{userId}或email:{ www.example.com },正确的方式是userid:1234或email:email。com(你不需要{})。

此外,我提供了一个JSON数组的base64编码版本,它有5个不必要的属性,我只需要userid:{userId}或email:{email}。
感谢CBroe的评论和帮助。

<?php

namespace App\Services;

use GuzzleHttp\Client;

class AdobeSign
{
    private $client;
    private $baseUrl;
    private $integrationKey;
    private $secretKey;
    private $userId;

    public function __construct()
    {
        $this->client = new Client();
        $this->baseUrl = "https://api.eu1.echosign.com/api/rest/v6";/*URL DE LA API*/
        $this->integrationKey = env('ADOBE_SIGN_INTEGRATION_KEY');/*De la cuenta de Adobe Sign*/
        $this->secretKey = env('ADOBE_SIGN_SECRET_KEY');/*De la cuenta de Adobe Sign*/
        $this->userId = env('ADOBE_SIGN_USER_ID');/*De la cuenta de Adobe Sign*/
    }

    private function getAuthorizationHeader()
    { 
        $headers = [
            'Content-Type' => 'application/json',
            'Access-Control-Allow-Origin' => '*',
            'Access-Control-Allow-Methods' => 'GET, POST, PUT, DELETE, OPTIONS',
            'Authorization' => sprintf('Bearer %s', env('ADOBE_SIGN_TOKEN')),
            'x-api-user' => 'email:mail@mail.com'
        ];

        $timeStamp = time();
        $headers['x-sdk-date'] = gmdate('D, d M Y H:i:s T', $timeStamp);
        
        print_r($headers);
        echo(base64_encode(json_encode($headers)));
        return $headers;
    }

    private function getAccessToken($timeStamp)
    {
        $jwtPayload = [
            'iss' => $this->integrationKey,
            'sub' => base64_encode(json_encode(env('ADOBE_SIGN_USER_EMAIL'))),
            'aud' => 'https://api.eu1.echosign.com',
            'exp' => $timeStamp + 3600,
            'iat' => $timeStamp
        ];

        $jwtHeader = [
            'alg' => 'HS256',
            'typ' => 'JWT'
        ];

        $jwtHeaderEncoded = base64_encode(json_encode($jwtHeader));
        $jwtPayloadEncoded = base64_encode(json_encode($jwtPayload));
        $jwtSignatureEncoded = base64_encode(hash_hmac('sha256', "$jwtHeaderEncoded.$jwtPayloadEncoded", $this->secretKey, true));

        $jwtToken = "$jwtHeaderEncoded.$jwtPayloadEncoded.$jwtSignatureEncoded";

        return $jwtToken;
    }

    public function sendAgreement($name, $email, $fileUrl, $X, $Y, $page)
    {
        $response = $this->client->post($this->baseUrl . '/agreements', [
            #HEADER
            'headers' => $this->getAuthorizationHeader(),
            #BODY
            'json' => [
                'documentCreationInfo' => [
                    'fileInfos' => [/*Documento pdf INFO*/
                        [
                            'libraryDocumentId' => '',/*Se utiliza si ya esta en la biblioteca*/
                            'transientDocumentId' => '',/*Se utiliza si el archivo aún no se ha subido a la biblioteca*/
                            'documentURL' => $fileUrl,/*URL DEL ARCHIVO*/
                            'name' => $name/*nombre del archivo*/
                        ]
                    ],
                    'recipientSetInfos' => [/*Destinatario INFO*/
                        [
                            'recipientSetMemberInfos' => [/*Info, por si se quieren añadir mas de un destinatario */
                                [
                                    'email' => $email/*Email del destinatario*/
                                ]
                            ],
                            'recipientSetRole' => 'SIGNER'/*ROL DEL DESTINATARIO [SIGNER :=: Firmante]*/
                        ]
                    ],
                    'signatureType' => 'ESIGN', /*TIPO DE FIRMA QUE SE UTILIZARÁ [ESIGN :=: Puede firmar el acuerdo digitalmente]*/
                    'signatureFlow' => 'SENDER_SIGNATURE_NOT_REQUIRED',/*FLUJO DE LA FIRMA [SENDER_SIGNATURE_NOT_REQUIRED :=: El que lo envia necesita firmar el acuerdo]*/
                    'name' => $name,/*NOMBRE DEL ACUERDO QUE SE VA A FIRMAR */
                    'signatureFieldInfos' => [
                        [
                            'defaultValue' => '',
                            'locations' => [
                                [
                                    'height' => $Y,
                                    'left' => $X,
                                    'pageNumber' => $page,
                                    'top' => '50',
                                    'width' => '100'
                                ]
                            ],
                            'name' => 'Signature'
                        ]
                    ]
                ]
            ]
        ]);
    }
}

相关问题