I am trying to use linkedin to get profile data of users, I get the authorization code from a button on login form that redirects to another page where I want to have some fields that fill with their name and such to create a user or login. I can't exchange the the auth token for an access one with my post. I get bad request or unauthorized responses. it says I am missing the required parameter "client_id", but it is obviously there ... Please take a look, thanks.
$code = $_GET['code'];
$data =array(
"grant_type" =>"authorization_code",
"code"=> $code,
"redirect_uri" => "http:\\localhost\index.php?doctor=linkedin",
"client_id" => "shh",
"client_secret" => "secret"
);
$str_data = json_encode($data);
//echo $str_data;
//function sendPostData($url, $post){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://www.linkedin.com/oauth/v2/accessToken');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$str_data);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));
curl_setopt($ch, CURLOPT_HEADERFUNCTION, "HandleHeaderLine");
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$result = curl_exec($ch);
$token = json_decode($result);
var_dump($token);
curl_close($ch); // Seems like good practice
function HandleHeaderLine( $curl, $header_line ) {
echo "<br>".$header_line; // or do whatever
return strlen($header_line);
}
LinkedIn expects ones POSTed field, as a query string. Try changing:
$str_data = json_encode($data);
to:
$str_data = http_build_query($data);
An Example from their documentation :
POST /oauth/v2/accessToken HTTP/1.1
Host: www.linkedin.com
Content-Type: application/x-www-form-urlencoded
grant_type=authorization_code&code=987654321&redirect_uri=https%3A%2F%2Fwww.myapp.com%2Fauth%2Flinkedin&client_id=123456789&client_secret=shhdonottell
Hope that helps.
Related
I am trying to authenticate, the result of my request is returned incorrectly.
Request Model
Method: Post, Endpoint: /api/authenticate, Header Variables:
[{"key":"Content-Type","value":"application/json","enabled":true}],
Body Parameters: username: string, password: string,
authenticationType: string
Sample Request
POST /api/authenticate
Host: mpop-sit.hepsiburada.com
Content-Type: application/json
{
"username": "xyz_dev",
"password": "XYZ_dev123!",
"authenticationType": "INTEGRATOR"
}
Request i sent
$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array(
'Content-Type: application/json',
'Authorization: Bearer '. base64_encode('xyz_dev:XYZ_dev123!:INTEGRATOR'),
);
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);
This is the result of the query returned and the error I received.
Where do you think I might be making a mistake?
Array ( [timestamp] => 2020-02-07T09:01:47.426+0000 [status] => 500
[error] => Internal Server Error [exception] =>
io.jsonwebtoken.MalformedJwtException [message] => JWT strings must
contain exactly 2 period characters. Found: 0 [path] =>
//api/authenticate )
Do you need the authentication header for this endpoint?
Because what the sample request wants you to send the parameters in the request body like this:
$post = [
'username' => 'xyz_dev',
'password' => 'XYZ_dev123!',
'authenticationType' => 'INTEGRATOR'
];
$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
$ch = curl_init($url);
$header = array('Content-Type: application/json');
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$result = curl_exec($ch);
$return=json_decode($result,true);
print_r($return);
So no need fot the Authentication: Bearer ... header, it would also be created differently.
This should work fine! I get access denied error, so, that means codes works fine.
Note you might need to change http_build_query($fields) to $fields
$url = "https://mpop-sit.hepsiburada.com/api/authenticate/";
$token = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$fields = array(
"username" => "xyz_dev",
"password" => "XYZ_dev123!"
);
$header = array(
'Content-Type: application/json',
'Authorization' => 'Bearer ' . $token,
);
//open curl connection
$ch = curl_init();
//set the url, fields, vars
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, true);
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false); // SSL false if not required
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false); //False if not required
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
//execute fields
$result = curl_exec($ch);
//return result echo $result; if you need
echo curl_error($ch);
//close curl connection
curl_close($ch);
Please let me know if it works!
UPDATE : if you want to use ssl which is saving you from hacks.
Follow steps in this answer to activate ssl : https://stackoverflow.com/a/59919558/12232340
2 slashes here, so it does not work:
$url = 'https://mpop-sit.hepsiburada.com//api/authenticate';
This is my sample JSON Request
{
"Username":"admin",
"Password":"root123",
"PinCodeā : "hello321"
}
And also I want to post a request token as well, the request token has to be posted as a Request Header.
When I successfully sent those data and valid token, I need to receive a jSON response as below,
{
"Title": "Mr.",
"Name": "Pushkov",
"Age": "18"
}
My Question is,
How can I POST my above mentioned JSON data and the token in PHP cURL and display an error if the details are wrong in code igniter?
This is what I have done so far..and its not giving me the exact output im looking for and I couldn't figure out a way to send my token as a header request...
public function send_veri(){
$url='http://helloworld21.azurewebsites.net/api/member/login';
$ch = curl_init('http://localhost/apphome');
$data = array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode'));
$data_string = json_encode($data);
// Disable SSL verification
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
//curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
// Will return the response, if false it print the response
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Set the url
//curl_setopt($ch, CURLOPT_URL,$url);
// Execute
$result=curl_exec($ch);
}
once I run this, I could only get Username, Password and the PinCode only. I cannot post the request token in my header, in causing of that i cannot get the member detail response.
For Sending Data -
public function send_veri(){
$url='http://helloworld21.azurewebsites.net/api/member/login';
$ch = curl_init( $url );
$bodyData=json_encode(array()); // here you send body Data
$data = json_encode(array("Username" =>$this->input->post('un'), "Password" =>$this->input->post('pw'), "PinCode" =>$this->input->post('VerificationCode')));
curl_setopt( $ch, CURLOPT_POSTFIELDS, $bodyData );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array("headerdata: ".$data.",Content-Type:application/json"));
curl_setopt( $ch, CURLOPT_RETURNTRANSFER, true );
$result = curl_exec($ch);
curl_close($ch);
}
Get Header data on Apache Server Code -
$hEAdERS = apache_request_headers();
$data = json_decode($hEAdERS['headerdata'], true);
echo $contentType=$hEAdERS['Content-Type'];
echo $Username = #trim($data['Username']);
echo $Password = #trim($data['Password']);
echo $PinCode = #trim($data['PinCode']);
It works for me.
For Previous question about Azure account, I can create an app using azure account.
Now I can get auth code from below url:
Auth_code
From Auth_code we can get the access token by:
$auth_code = $_GET['code'];
$result = access($auth_code);
function access($auth_code){
$redirectUri = 'https://XXXX /authorize.php';
$token_request_data = array (
"grant_type" => "authorization_code",
"code" => $auth_code,
"redirect_uri" => $redirectUri,
"client_id" => "client_id",
"client_secret" => "client_secret",
"resource" =>"resource" (From manifest in azure)
);
$token_request_body = http_build_query ( $token_request_data );
$curl = curl_init ( 'https://login.windows.net/common/oauth2/token' );
curl_setopt ( $curl, CURLOPT_RETURNTRANSFER, true );
curl_setopt ( $curl, CURLOPT_POST, true );
curl_setopt ( $curl, CURLOPT_POSTFIELDS, $token_request_body );
curl_setopt ( $curl, CURLOPT_SSL_VERIFYPEER,false);
$response = curl_exec ( $curl );
$res = json_decode($response);
curl_close ( $curl );
Now I'm trying to access the web api using that access_token,I couldn't get the result.
For example:
$authHeader = 'Authorization:Bearer access_toke';
$ch = curl_init();
$url = 'https://domain/api/data/v8.0/contacts';
$curl = curl_init();
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, true);
curl_setopt($curl, CURLOPT_POST, false);
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, 'GET');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json'));
$result = curl_exec($curl);
echo "<pre>";print_r($result);exit;
curl_close($curl);
I'm getting empty response. Now I have to know how to access the web API using access token.
When I try to run manually https://domain/api/data/v8.0/contacts, I can get all contacts in my crm.But when I try to access it by access_token using php,it returns empty.
Web api reference url : reference for web api url
Refer to the guide Compose HTTP requests and handle errors, as the requirements shown at HTTP headers section:
Every request should include the Accept header value of application/json, even when no response body is expected.
And there are supernumerary blank in your PHP script at curl_setopt($curl, CURLOPT_HTTPHEADER, array($authHeader, 'Content- Type:application/json'));
You can remove the blanks in Content-Type and try again.
By the way, you can leverage Fiddler to capture the requests from your PHP client. We you get the response body content and request status code via the tool. We can match the status code with the list at https://msdn.microsoft.com/en-us/library/gg334391.aspx#bkmk_statusCodes. If the code is always 200 without response body, you may check your code in web api.
I have built a prototype calendar synching system using the Google calendar API and it works well, except refreshing access tokens. These are the steps I have gone through:
1) Authorised my API and received an authorisation code.
2) Exchanged the authorisation code for Access Token and a RefreshToken.
3) Used the Calendar API until the Access Token expires.
At this point I try to use the Refresh Token to gain another Access Token, so my users don't have to keep granting access because the diary sync happens when they are offline.
Here's the PHP code, I'm using curl requests throughout the system.
$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = array("grant_type" => "refresh_token",
"client_id" => $clientID,
"client_secret" => $clientSecret,
"refresh_token" => $refreshToken);
$headers[0] = 'Content-Type: application/json';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($postData));
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
The response i'm getting is:
[error] => invalid_request
[error_description] => Required parameter is missing: grant_type
No curl errors are reported.
I've tried header content-type: application/x-www-form-urlencoded, and many other things, with the same result.
I suspect it's something obvious in my curl settings or headers as every parameter mentioned in the Google documentation for this request is set. However, I'm going around in circles so would appreciate any help, including pointing out any obvious errors I've overlooked.
your request should not post JSON data but rather query form encoded data, as in:
$requestURL = "https://accounts.google.com/o/oauth2/token";
$postData = "grant_type=refresh_token&client_id=$clientID&client_secret=$clientSecret&refresh_token=$refreshToken";
$headers[0] = 'Content-Type: application/x-www-form-urlencoded';
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_URL, $requestURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postData);
$response = curl_exec($ch);
$responseArray = json_decode($response, TRUE);
Today I tried to post a like via PHP (Curl) without luck. The output is that a token is required but I used a working token. I tried the same token with JS and it works.
Did Instagram changed some things bout PHP?
Here is my code:
<?php
$media_id = '615839918291043487_528338984';
$url = "https://api.instagram.com/v1/media/615839918291043487_528338984/likes?";
$access_token_parameters = array(
'access_token' => '191573449.9e262d9.ff708911edcd4f809ca31dd76d08c0ba',
'action' => 'like'
);
$curl = curl_init($url);
curl_setopt($curl,CURLOPT_GET,true);
curl_setopt($curl,CURLOPT_GETFIELDS,$access_token_parameters);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
echo curl_exec($curl);
?>
Output.:
{"meta":{"error_type":"OAuthParameterException","code":400,"error_message":"Missing client_id or access_token URL parameter."}}
I tried it on several server, with several proxies, several client and token. Hopefully you know what's going on.
To add a like to a photo you need to do it via POST. The following is an modified version of your code to do this.
<?php
$url = "https://api.instagram.com/v1/media/615839918291043487_528338984/likes";
$fields = array(
'access_token' => '191573449.9e262d9.ff708911edcd4f809ca31dd76d08c0ba'
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($fields));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
echo $response;
?>