Got unauthorized client error while refreshing access token - google-drive-api

Unauthorized client error while calling API https://www.googleapis.com/oauth2/v4/token
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
function GetRefreshedAccessToken($client_id, $refresh_token, $client_secret)
{
$url_token = 'https://www.googleapis.com/oauth2/v4/token';
$curlPost = 'client_id=' . $client_id . '&client_secret=' . $client_secret . '&refresh_token='. $refresh_token . '&grant_type=refresh_token';
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url_token);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_POSTFIELDS, $curlPost);
$data = json_decode(curl_exec($ch), true);
print_r($data);
exit;
$http_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);
if($http_code != 200)
{
throw new Exception('Error : Failed to refresh access token');
}
return $data;
}
add_action('wpcf7_before_send_mail', 'save_application_form');
function save_application_form($wpcf7) {
$client_id = 'xxx';
$refresh_token='xxx';
$client_secret='xxx';
global $wpdb;
$wpcf7 = WPCF7_ContactForm :: get_current() ;
$submission = WPCF7_Submission::get_instance();
if ($submission) {
$submited = array();
$submited['title'] = $wpcf7->title();
$submited['posted_data'] = $submission->get_posted_data();
$uploaded_files = $submission->uploaded_files();
$cf7_file_field_name = 'file-846';
$image_location = $uploaded_files[$cf7_file_field_name];
$test = pathinfo($image_location);
}
GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$token = GetRefreshedAccessToken($client_id, $refresh_token, $client_secret);
$url = "https://www.googleapis.com/upload/drive/v3/files?uploadType=media";
$headers = array(
"Content-Type: image/jpeg",
"Authorization: Bearer ".$token
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_BINARYTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_POST, true);
//$data1 = '#'.realpath('./metadata.json').';type=application/json;charset=UTF-8';
//$data2 = '#'.realpath('./test.csv').';type=text/csv';
$postdata = array(
'file' => file_get_contents($image_location)
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
$output = curl_exec($ch);
$err = curl_error($ch);
curl_close($ch);
if ($err) {
echo "cURL Error #:" . $err;
} else {
echo $output;
}
}
I want to refresh auth token automatically. But I got error unauthorized client error while calling API. Is it possible to upload media file on someones's google drive? For that I need refesh token. So It should not asking login again and again

Related

how to get json data from 8080 port in php

I am using CURL function in php.
i get json data from 8080 port , but return only empty value..
how to get json data from 8080 port
below the link are using in CURL functions...
<?php
$url = 'https://example.com:8080/getdata/?format=json';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headr);
curl_setopt($ch, CURLOPT_PORT, 8080);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
$result = curl_exec($ch);
echo $result;
?>

Error: Skin null does not exist

From my controller, I'm trying to make a post request to https://test.adyen.com/hpp/select.shtml but I continue receiving this error
Error: Skin null does not exist
This is part of my code
$paramsRequest = array (
'merchantReference' => 'reference',
'paymentAmount' => 10,
'currencyCode' => 'EUR',
'shipBeforeDate' => '2017-11-29T13:42:40+1:00',
'skinCode' => 'f8My4RJC',
'merchantAccount' => 'TestNL076',
'sessionValidity' => '2017-11-29T13:42:40+1:00',
'merchantReturnData' => 'string',
'shopperEmail' => 'shopper#gmail.com',
);
$hcma = '8382E2...';
$paramsRequest['merchantSig'] = Util::calculateSha256Signature($hcma, $paramsRequest);
return $this->handlePostRequest($paramsRequest, 'https://test.adyen.com/hpp/select.shtml');
and to handle the request
protected function handlePostRequest($data, $url)
{
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLOPT_USERPWD, 'admin' . ":" . 'password');
curl_setopt($ch, CURLOPT_POST, count($data));
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $result;
}
Are you trying to perform a lookup of available payments? If so, use the end point.
https://test.adyen.com/hpp/directory.shtml
Otherwise the select.shtml an endpoint to redirect shopper's browser when you want them to enter in information or send them to their bank. Here's link to adyen's local payment method docs.

PHP Curl POST with JSON returns Server Error 500 - Django

I am using the code below to make PHP Curl POST request. Could someone tell me , why the server sends me Server Error Code 500? When open the api link in the browser and test it with json data everything is ok. I use the similar code to make PUT request and I don`t have problems.
$postArray = array(
"email" => $email
);
$json = json_encode( $postArray, JSON_PRETTY_PRINT );
$pl4m_url = "api-link";
$ch = curl_init($pl4m_url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,$json);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
$result = curl_exec($ch);
curl_close($ch);
echo $result;

convert mysql to xml and encode to json

help me please, i have problem with my code, dan saya pusing sekali tolong bantu saya
view my code :
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"postvar1=value1&postvar2=value2&postvar3=value3");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$server_output = curl_exec ($ch);
curl_close ($ch);
// further processing ....
if ($server_output == "OK") { ... } else { ... }

post json data with php curl for multipart/form-data for file upload vía cakephp 2

i want to post json data with php curl for a multipart/form-data with a file upload field.
i tried this in cakephp 2 in a action:
public function json_post(){
$this->autoRender = false;
debug('json post test');
$data = array('Post' => array(
'subject' => 'test subject content',
'body' => 'test body content'
'fileName' => '/Users/mar/Pictures/cow_wall_90.jpg'
));
$data_string = json_encode($data);
$ch = curl_init('http://www.mydomain.com/posts/phone_upload.json');
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
//'Content-Type: multipart/form-data',
'Content-Type: application/json',
'Content-Length: ' . strlen($data_string))
);
$result = curl_exec($ch);
debug($result);
}
fileName is the equivalent for the file upload field in a form.
subject and body are the equivalent for text fields in a form.
i miss something may be in the data array or in the Content-Type but can't find the problem.
thanks for any help, regards, martin
From http://dtbaker.net/web-development/uploading-a-file-using-curl-in-php/:
notice the # infront of the file path, this is the magic part.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_VERBOSE, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
curl_setopt($ch, CURLOPT_POST, true);
// same as <input type="file" name="file_box">
$post = array(
"file_box"=>"#/path/to/myfile.jpg",
);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
?>
Seems you were missing the magic part.
You may also switch from cURL to CakePHP's HttpSocket, but that's just personal prefrence.
http://book.cakephp.org/2.0/en/core-utility-libraries/httpsocket.html