jeudi 2 avril 2020

How do I pass apikey and other keys to header in guzzle 6.3?

I have a simple registration form that the user can register in my app, now I want to send submitted data to another service.

First I test my request using postman as follows using a raw option in a postman panel.

Api url : app3.salesmanago.pl/api/contact/upsert

JSON DATA:
{
  "clientId":"w2ncrw06k7ny45umsssc",
  "apiKey":"ssssj2q8qp4fbp9qf2b8p49fz",
  "requestTime":1327056031488,
  "sha":"ba0ddddddb543dcaf5ca82b09e33264fedb509cfb4806c",
  "async" : true,
  "owner" : "adam@rce.com",
  "contact" : { 
        "email" : "test-1@konri.com",
        "name" : "Test",
        "address":{
            "streetAddress":"Brzyczynska 123",
      }
    }
}

I get the following success result

{
    "success": true,
    "message": [],
    "contactId": "b52910be-9d22-4830-82d5-c9dc788888ba",
    "externalId": null
}

Now using guuzle htpp request in laravel

 protected function create(array $data)
    {
        $user = User::create([
            'name' => $data['name'],
            'email' => $data['email'],
            'password' => bcrypt($data['password']),
            ]);

        $current_timestamp = Carbon::now()->timestamp;
        $client = new Client();
        $request = $client->post('app3.salesmanago.pl/api/contact/upsert', [
            \GuzzleHttp\RequestOptions::JSON => [        
            'headers' => [
                'Accept' => 'application/json, application/json',
                'Content-Type' => 'application/json',
                'clientId' => 'dd2ncrw06k7ny45umce',
                'apiKey' => 'ddjdd2q8qp4fbp9qf2b8p49fdzd',
                'sha' => ' wba0b543dcaf5ca82b09e33264fedb4509cfb4806ec',
                "requestTime" => $current_timestamp,
                "owner" => "testemail@wp.com",
            ],
            'form_params' => [
                'name' => $data['name'],
                'email' => $data['email'],
            ]
          ]
        ]);

        $response = $request->getBody();
        $r = json_decode($response);
        dd($r);
        return $user;
    }

When I run my app and send the form data I get this using the same data as in postman I get this

{#306 ▼
  +"success": false
  +"message": array:1 [▼
    0 => "Not authenticated"
  ]
  +"contactId": null
  +"externalId": null
}

It seems like my API key and other header data are not passed to the header as required,

Can someone tell me what am I doing wrong here?



via Chebli Mohamed

Aucun commentaire:

Enregistrer un commentaire