How to create a Bearer token for GOOGLE CLOUD JSON API - google-cloud-storage

To upload objects to google cloud storage buckets, I need an authentication token. I have chosen JSON API method to upload objects to buckets.
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "https://www.googleapis.com/upload/storage/v1/b/Mybucket/o?uploadType=media");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "yourdata");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
$headers = array();
$headers[] = "Authorization:Bearer <TOKEN>";
$headers[] = "x-goog-project-id: xxxxxxxxxxx";
$headers[] = "x-goog-user-project: xxxxxxxxxxx";
$headers[] = "Content-Length: 8";
$headers[] = "Content-Type: image/jpeg";
$headers[] = "x-goog-acl: public-read-write";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
?>
Is there any way to upload object without these type of
authentication or any other authentication method?
How to generate this token (bearer) programmatically using api/curl/php?

function getOAuthToken(){
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, "http://metadata.google.internal/computeMetadata/v1/instance/service-accounts/default/token");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "GET");
$headers = array();
$headers[] = "Metadata-Flavor: Google";
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
// echo 'Error:' . curl_error($ch);
}
curl_close ($ch);
$result = json_decode($result,true);
return $result['access_token'];
}
$result['access_token'] is the bearer token.

Related

Some of the aliases you requested do not exist:

I am getting the following response from the Facebook Ad Library API
{"error":{"message":"(#803) Some of the aliases you requested do not exist: \u003CAPI_VERSION>","type":"OAuthException","code":803,"fbtrace_id":"AXZ00I_mwgTvJwqIImb1SBw"}}
Below is the CURL I am trying to execute;
<?php
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, 'https://graph.facebook.com/<API_VERSION>/ads_archive');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, "search_terms='california'&ad_type=POLITICAL_AND_ISSUE_ADS&ad_reached_countries=['US']&access_token=330479468051422|t_w8UDcd2QYqQ_hfxfs-LsKSxIA");
$headers = array();
$headers[] = 'Content-Type: application/x-www-form-urlencoded';
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
$result = curl_exec($ch);
if (curl_errno($ch)) {
echo 'Error:' . curl_error($ch);
}
curl_close($ch);
echo $result;
?>
Is there anything you can see that I have done wrong, much apprecaited thankyou.

Invoke-RestMethod 2 PHP_Curl

In PS I made this:
$body=#{
'code'='$code'
'access_token'=$token
}
$result = Invoke-RestMethod -Uri $uri -Method Post -Body $body
Works perfect. How can I do the same in PHP Curl ?
Ive tryed something like this:
$data = array("code" => $code, "access_token" => $token);
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $uri );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt($ch, CURLOPT_POST, 1 );
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
"code=" . $code,
"access_token=" . $token
));
curl_setopt($ch, CURLOPT_HTTPHEADER,
array('ContentType:text/plain'));
$result=curl_exec ($ch);
print_r($result);
But I keep getting error like : Parameter token is missing.

how to secure username password when calling remote restful api in web/mobile?

here is my script of php to call remote restful api, the username password are hard coded in source code,
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,"http://localhost/m1/m1apiserver/public/api/login");
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS,
"email=elson%40bla.com&password=bla");
// receive server response ...
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$json = curl_exec ($ch);
curl_close ($ch);
$j = json_decode($json);
// further processing ....
if ($j->token) {
//echo $j->token;
$output = file_get_contents("http://localhost/m1/m1apiserver/public/api/product?token=".$j->token);
echo $output;
}
but are there any techniques to secure the username password?

Why did Implementing HTTP/1.1 break my IPN script?

Here is the original code I had in my PHP script:
$header = "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
I got an email from Paypal saying that I needed to upgrade my IPN script so that it uses HTTP/1.1. So here is what I changed my code to, based on their directions:
$header .="POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .="Content-Type: application/x-www-form-urlencoded\r\n";
$header .="Host: www.paypal.com\r\n";
$header .="Connection: close\r\n";
Payments have gone through today, but the IPN is no longer updating my database and this is the only change I made to it. Any ideas on what to do?
Thanks!
Yes. I have just done battle with this. What worked for me was to remove the connection close header, and add a trim to the response back from PP. Here are the headers:
$header = "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Host: www.paypal.com\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
Here is the fsockopen:
$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30);
and here is the trim on the response back from PP:
if (!$fp) {
// HTTP ERROR
error_mail("Could not open socket");
//
} else {
fputs ($fp, $header . $req);
while (!feof($fp)) {
$res = trim(fgets ($fp, 1024));
}
//
// check the payment_status is Completed
// check that receiver_email is your Primary PayPal email
//
if ((strcmp ($res, "VERIFIED") == 0) && ($payment_status == "Completed") && ($receiver_email == $valid_receiver_email)) {
That worked for me.

PayPal IPN Help (Not UPDATE Database)

Hi there all been trying to get this working for about 2 days now. The Payment is going through no problem but it not update the Database :(
Here is the Code :
<?php
require_once(WWW_DIR."/lib/users.php");
// Connect to database
$con = mysql_connect("localhost","root","******");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
// read the post from PayPal system and add 'cmd'
$req = 'cmd=_notify-validate';
foreach ($_POST as $key => $value)
{
$value = urlencode(stripslashes($value));
$req .= "&$key=$value";
}
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
$fp = fsockopen ('ssl://www.sandbox.paypal.com', 443, $errno, $errstr, 30); // Test
//$fp = fsockopen ('ssl://www.paypal.com', 443, $errno, $errstr, 30); // Live
// assign posted variables to local variables
$item_name = $_POST['item_name'];
$payment_status = $_POST['payment_status'];
$payment_amount = $_POST['mc_gross'];
$txn_id = $_POST['txn_id'];
$payer_email = $_POST['payer_email'];
$name = $_POST['first_name'] . " " . $_POST['last_name'];
$address_street = $_POST['address_street'];
$address_zip = $_POST['address_zip'];
$address_city = $_POST['address_city'];
$contact_phone = $_POST['contact_phone'];
$email = $_POST['payer_email'];
$uid = $users->currentUserId();
//timezone set
date_default_timezone_set('Europe/London');
if (!$fp)
{
// HTTP ERROR
} else
{
fputs($fp, $header . $req);
while (!feof($fp))
{
$res = fgets($fp, 1024);
if (strcmp($res, "VERIFIED") == 0) {
if($payment_status != "Completed"){
if($payment_amount == 2.00)
{
$role = 6;
}
else
if($payment_amount == 5.00)
{
$role = 8;
}
else
if($payment_amount == 8.00)
{
$role = 9;
}
//update user records
$sql = sprintf("UPDATE users SET role = %d WHERE ID = %d", $role, $uid);
mysql_query($sql);
//log payment
//mysql_query("INSERT INTO payments (email, item_name, payment_status, txn_id, payment_ammount) VALUES('" . mysql_escape_string($email) . "', '" . $item_name . "', '" . $payment_status . "', '" . $txn_id . "', '" . $payment_amount . "' ) ") or die(mysql_error());
}} else
if (strcmp($res, "INVALID") == 0)
{
// log for manual investigation
}
}
fclose($fp);
}
?>
If you have any Questions just ask I will try and Answer ASAP if you could get this working I would be very happy
Thanks
Change:
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.0\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n\r\n";
To:
// post back to PayPal system to validate
$header .= "POST /cgi-bin/webscr HTTP/1.1\r\n";
$header .= "Host: www.sandbox.paypal.com\r\n";
$header .= "Content-Type: application/x-www-form-urlencoded\r\n";
$header .= "Content-Length: " . strlen($req) . "\r\n";
$header .= "Connection: close\r\n\r\n";
Change:
if (strcmp($res, "VERIFIED") == 0) {
To:
if (strcmp(trim($res), "VERIFIED") == 0) {
And lastly, change:
if (strcmp($res, "INVALID") == 0)
To:
if (strcmp(trim($res), "INVALID") == 0)
Reason:
The PayPal Sandbox is configured to reject HTTP 1.0 requests without a Host header specified, so your current script will never properly validate.