anjana1
(Anjana Asok)
October 26, 2021, 7:35am
1
Hi,
I am trying to authorize my application using API Key from a PHP application . I have the below code. What else am i missing below as its asking for credetails.
$search_host = 'lux.es.eastus2.azure.elastic-cloud.com'; $search_port = '9243'; $index = 'testing12'; $doc_type = '_doc'; $doc_id = 1; $json_doc = array( "user" => "kimchy", "post_date" => "2012-11-15T14:12:12", "message" => "trying out Elastic Search021" ); $json_doc = json_encode($json_doc); $baseUri = 'https://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id; $ci = curl_init(); curl_setopt($ci, CURLOPT_URL, $baseUri); // $headers = array(); // $headers[] = 'Content-Type: application/json, Authorization: WXV3dHUzd0JaUTFVSEYwVDBqZmI6MTVjSjl5bFdUdjZQWm1oZHJMaGhGZw=='; $apiKey = ''; curl_setopt($ci, CURLOPT_HTTPHEADER, array( 'Authorization: ' . $apiKey, 'Content-Type: application/json' )); //curl_setopt($ci, CURLOPT_HTTPHEADER, $headers); curl_setopt($ci, CURLOPT_PORT, $search_port); curl_setopt($ci, CURLOPT_TIMEOUT, 200); curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc); $response = curl_exec($ci); if ($response === false) { $info = curl_getinfo($ci); curl_close( $ci); die('error occured during curl exec. Additioanl info: ' . var_export($info)); } curl_close( $ci); $decoded1 = json_decode($response,true); if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') { die('error occured: ' . $decoded1->response->errormessage); } echo 'response ok!'; var_export($decoded1);
warkolm
(Mark Walkom)
October 26, 2021, 8:06am
2
Can you please reformat your code/logs/config using the </>
button, or markdown style back ticks. It helps to make things easy to read which helps us help you
TimV
(Tim Vernum)
October 29, 2021, 5:08am
4
Sorry, your code doesn't have any new lines in it, so it's impossible to read.
And you've commented out some parts of the code with //
so we need the newlines in order to know where the comments end and the code starts again.
2 Likes
anjana1
(Anjana Asok)
November 1, 2021, 6:21am
5
$search_host = 'lux.es.eastus2.azure.elastic-cloud.com';
$search_port = '9243';
$index = 'testing12';
$doc_type = '_doc';
$doc_id = 1;
$json_doc = array( "user" => "kimchy",
"post_date" => "2012-11-15T14:12:12",
"message" => "trying out Elastic Search021" );
$json_doc = json_encode($json_doc);
$baseUri ='https://'.$search_host.':'.$search_port.'/'.$index.'/'.$doc_type.'/'.$doc_id; $ci = curl_init();
curl_setopt($ci, CURLOPT_URL, $baseUri);
// $headers = array();
// $headers<ins>[</ins><ins>]</ins> = 'Content-Type: application/json, Authorization: WXV3dHUzd0JaUTFVSEYwVDBqZmI6MTVjSjl5bFdUdjZQWm1oZHJMaGhGZw=='; $apiKey = '';
curl_setopt($ci, CURLOPT_HTTPHEADER, array( 'Authorization: ' . $apiKey, 'Content-Type: application/json' ));
//curl_setopt($ci, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ci, CURLOPT_PORT, $search_port); curl_setopt($ci, CURLOPT_TIMEOUT, 200);
curl_setopt($ci, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ci, CURLOPT_FORBID_REUSE, 0); curl_setopt($ci, CURLOPT_CUSTOMREQUEST, 'POST'); curl_setopt($ci, CURLOPT_POSTFIELDS, $json_doc);
$response = curl_exec($ci);
if ($response === false) { $info = curl_getinfo($ci);
<ins>curl_clo</ins><ins>se</ins>( $ci);
die('error occured during curl exec. Additioanl info: ' . var_export($info)); }
curl_close( $ci);
$decoded1 = json_decode($response,true);
if (isset($decoded1->response->status) && $decoded1->response->status == 'ERROR') { die('error occured: ' . $decoded1->response->errormessage); }
echo 'response ok!';
var_export($decoded1);
anjana1
(Anjana Asok)
November 1, 2021, 7:31am
7
Its the API key genreated via Cloud-> Features ->API keys
We are trying to authorize using API Key .
TimV
(Tim Vernum)
November 2, 2021, 4:43am
8
The API Keys that you generate in the Cloud UI will only provide access to the Elastic Cloud API (api.elastic-cloud.com
). You cannot use them to access a deployment.
For access to individual deployments, you need to create an API key within that deployment.
1 Like
system
(system)
Closed
November 30, 2021, 4:43am
9
This topic was automatically closed 28 days after the last reply. New replies are no longer allowed.