I couldn't fetch user's email address through linkedin v2 api. I added r_emailaddress permission in app settings and also in access token request as well. But it says.
{
"serviceErrorCode": 100,
"message": "Not enough permissions to access: GET-members /clientAwareMemberHandles",
"status": 403
}
My request url is:
https://api.linkedin.com/v2/clientAwareMemberHandles?q=members&projection=(elements*(primary,type,handle~))&oauth2_access_token=".$token
Anyone please help me to solve this.
You can access the linkedin user email address with below EP:
`https://api.linkedin.com/v2/emailAddress?q=members&projection=(elements*(handle~))`
Make sure that you have defined the scope 'r_emailaddress' in your library.
You can use below curl request to fetch the data:
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $Url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Authorization: Bearer '. $token,
'X-Restli-Protocol-Version: 2.0.0',
'Accept: application/json',
'Content-Type: application/json'
));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
$response = curl_exec($ch);
$headerSize = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$body = substr($response, $headerSize);
$response_body = json_decode($body,true);
$response_body will return the following response:
Array (
[elements] => Array
(
[0] => Array
(
[handle] => urn:li:emailAddress:123456
[handle~] => Array
(
[emailAddress] => your#email.in
)
)
)
)
With this type of response "handle~" value is not easy to get the next step should be :
$email = [];
$response_body = json_decode($res->getBody());
$object = $response_data->elements[0];
foreach ($object as $value){
$email[] = $value->emailAddress;
}
print_r($email[0]);
Related
I am creating a PHP script to access Open Ai's API, to ask a query and get a response.
I am getting the following error:
You didn't provide an API key. You need to provide your API key in an
Authorization header using Bearer auth (i.e. Authorization: Bearer
YOUR_KEY)
...but I thought I was providing the API key in the first variable?
Here is my code:
$api_key = "sk-U3B.........7MiL";
$query = "How are you?";
$url = "https://api.openai.com/v1/engines/davinci/jobs";
// Set up the API request headers
$headers = array(
"Content-Type: application/json",
"Authorization: Bearer " . $api_key
);
// Set up the API request body
$data = array(
"prompt" => $query,
"max_tokens" => 100,
"temperature" => 0.5
);
// Use WordPress's built-in HTTP API to send the API request
$response = wp_remote_post( $url, array(
'headers' => $headers,
'body' => json_encode( $data )
) );
// Check if the API request was successful
if ( is_wp_error( $response ) ) {
// If the API request failed, display an error message
echo "Error communicating with OpenAI API: " . $response->get_error_message();
} else {
// If the API request was successful, extract the response text
$response_body = json_decode( $response['body'] );
//$response_text = $response_body->choices[0]->text;
var_dump($response_body);
// Display the response text on the web page
echo $response_body;
All Engines endpoints are deprecated.
This is the correct Completions endpoint:
https://api.openai.com/v1/completions
Working example
If you run php test.php in CMD, the OpenAI API will return the following completion:
string(23) "
This is indeed a test"
test.php
<?php
$ch = curl_init();
$url = 'https://api.openai.com/v1/completions';
$api_key = '<OPENAI_API_KEY>';
$post_fields = '{
"model": "text-davinci-003",
"prompt": "Say this is a test",
"max_tokens": 7,
"temperature": 0
}';
$header = [
'Content-Type: application/json',
'Authorization: Bearer ' . $api_key
];
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post_fields);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error: ' . curl_error($ch);
}
curl_close($ch);
$response = json_decode($result);
var_dump($response->choices[0]->text);
?>
I'm trying to add a user using the POST api/user api request and get the following response (via PHP):
403 - access denied exception - No access to this type of entities
I'm not sure what this means. I'm using the example data set for this with the following php code - just trying to get the basic functionality working:
$ch = curl_init();
$header = array();
$post = array(
'data' => array(
"type"=>"users",
"attributes"=>array(
"username"=>"testuser",
"email"=> "testuser#oroinc.com",
"firstName"=> "Bob",
"lastName"=> "Fedeson",
"password"=> "Password000!"
),
"relationships"=>array(
"owner"=>array(
"data"=>array(
"type"=> "businessunits",
"id"=> "1"
)
)
),
)
);
$header[] = 'Accept: application/vnd.api+json';
$header[] = 'Authorization: WSSE profile="UsernameToken"';
$header[] = $wsseHeader;
curl_setopt($ch, CURLOPT_URL,"https://[full url here]/api/users");
curl_setopt($ch, CURLOPT_TIMEOUT, 60);
curl_setopt($ch, CURLOPT_HTTPHEADER,$header);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$rest = curl_exec($ch);
if($rest === false)
{
echo 'Curl error: ' . curl_error($ch);
}else{
$rest = json_decode($rest, true);
echo '<pre>';
var_dump($rest);
echo '</pre>';
}
403 - access denied exception means that your user has not enough access permissions. The server understood the request but is refusing to fulfill it. The authorization will not help and the request SHOULD NOT be repeated.
To grant access to add new entity objects you should set "Create" permission of your entity to the required level (global/.../user) in the "System/User Management/Roles"
I am receiving a "INVALID_BODY" error with the message "body could not be parsed as JSON" when sending a curl request through php to create a plaid link token.
I have the header and body formatted this way:
$ch=curl_init("https://development.plaid.com/link/token/create");
$username = array(
"client_user_id"=>"cus_L7tpXAO0PXsPsh"
);
$headers = array(
'Content-type: application/json'
);
$data = array(
'client_id'=>'ID',
'secret'=>'SECRET',
'client_name'=>'Plaid App',
'user'=>$username,
'products'=>'auth',
'country_codes'=>'US',
'language'=>'en',
'webhook'=>'https://webhook.sample.com'
);
$hstring = http_build_query($headers);
$string = http_build_query($data);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$token = curl_exec($ch);
echo $token;
$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
?>
There is probably a very obvious formatting issue but I can't see it as is. Appreciate any suggestions or criticisms.
I should also mention building out the POST in Postman also gives invalid body error.
I was getting the same error, but for a different reason. The problem in your code is how you are sending your country codes and products. They are expected to be arrays of strings despite the documentation seeming to say otherwise. Also, I don't think you're sending the data in JSON either... Try this:
$data =[
"client_id" => $plaidID,
"secret" => $plaidSecret,
"client_name" => $clientName,
"user" => [
"client_user_id" => $userID
],
"products" => ["auth"],
"country_codes"=>["US"],
"language" => "en"
];
$ch = curl_init();
curl_setopt($ch,CURLOPT_URL,"https://".$apiMode.".plaid.com/link/token/create");
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch,CURLOPT_HTTPHEADER,["content-type: application/json"]);
curl_setopt($ch,CURLOPT_POSTFIELDS,json_encode((object)$data,JSON_HEX_APOS | JSON_HEX_QUOT ));
$response = curl_exec($ch);
I am trying to make a simple CURL call to GetReponse using PHP and I must be doing something wrong. Each time I try to use my clients access token it bombs out. If I hard code my company API key into the place where I've put the xxxxx's it works fine. I'm using their docs, but I can't get it to work, any help? Btw, their docs are HORRIBLE - so bad I can't even begin to fully explain! They're filled with a billion typos... Their Docs
$url = "https://api.getresponse.com/v3/campaigns";
$headers = array();
$headers[] = "X-Auth-Token: api-key xxxxxxxxx";
$state_ch = curl_init();
curl_setopt($state_ch, CURLOPT_URL, $url);
curl_setopt($state_ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($state_ch, CURLOPT_HTTPHEADER, $headers);
$state_result = curl_exec ($state_ch);
$state_result = json_decode($state_result);
$debug = 1;
print_r($state_result);
I always get the same response:
stdClass Object
(
[httpStatus] => 401
[code] => 1014
[codeDescription] => Problem during authentication process, check headers!
[message] => Unable to authenticate request. Check credentials or authentication method details
[moreInfo] => https://apidocs.getresponse.com/en/v3/errors/1014
[context] => stdClass Object
(
[authenticationType] => auth_token
)
[uuid] => xxxxxxxxxxxxxxxxxxxxxxxxx
)
Again, if I put my company API key in the place of the xxxxxx's (which I have to get inside of their control panel) it works. Access tokens do not.
Solution:
Looks like the header needs to change to this...
$headers[] = "Authorization: Bearer xxxxxxxxxxxxxxx"
As I can see:
[httpStatus]401 = unauthorized
Check your API token permission
I'm using next code:
$headers = [];
$headers[] = "X-Auth-Token: api-key MY_API_KEY";
$ch = curl_init('https://api.getresponse.com/v3/campaigns');
curl_setopt($ch, CURLOPT_HEADER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$result = curl_exec($ch);
if($result)
{
$header_size = curl_getinfo($ch, CURLINFO_HEADER_SIZE);
$header = substr($result, 0, $header_size);
$body = substr($result, $header_size);
}
curl_close($ch);
I am using FCM to send push notification to IOS app, IOS dev provide me device token and SERVER_KEY, I search on google and tried different solutions but nothing worked for me , So let me know what is wrong with my code, Getting error {
"multicast_id": 8569689262516537799,
"success": 0,
"failure": 1,
"canonical_ids": 0,
"results": [
{
"error": "InvalidRegistration"
}
]
}
Thanks in advance
$ch = curl_init("https://fcm.googleapis.com/fcm/send");
//The device token.
$token = "c2420d68a0838d8fb6b26ef06278e899de73a149e93c9fe13df11f70f3dd5cc1"; //token here
//Title of the Notification.
$title = "Carbon";
//Body of the Notification.
$body = "Bear island knows no king but the king in the north, whose name is stark.";
//Creating the notification array.
$notification = array('title' => $title, 'text' => $body);
//This array contains, the token and the notification. The 'to' attribute stores the token.
$arrayToSend = array('to' => $token, 'notification' => $notification, 'priority' => 'high');
//Generating JSON encoded string form the above array.
$json = json_encode($arrayToSend);
//print_r($json); die();
//Setup headers:
$headers = array();
$headers[] = 'Content-Type: application/json';
$headers[] = 'Authorization: key=AAAAx6P1Nz0:APA91bEqkVCA9YRw9gpUvmF8UOVrYJ5T8672wfS_I7UAT3dA0g1QS7z-Z4fpn8JMiJ5kFRz9ZGc2K64hKZG-4__PAUqm733hqNDuFCDv9'; // key here
//Setup curl, add headers and post parameters.
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
//curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
//Send the request
$response = curl_exec($ch);
//Close request
print_r($response);
curl_close($ch);
//return $response;
token number ur sending is most probably wrong. try "/topics/all" this will send notification to all the users registered.