In Google Group Admin-SDK how do I get the "code" parameter? - php

Simple question I am sure. But I am new to the Google Group Admin-SDK and I want to authenticate using php and CURL:
$oauth2token_url = "https://accounts.google.com/o/oauth2/token";
$clienttoken_post = array(
"code" => $code,
"client_id" => $client_id,
"client_secret" => $client_secret,
"redirect_uri" => $redirect_uri,
"grant_type" => "authorization_code"
);
$curl = curl_init($oauth2token_url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $clienttoken_post);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$json_response = curl_exec($curl);
curl_close($curl);
$authObj = json_decode($json_response);
Most of this I understand, except I can't figure out where the value of the parameter "code" comes from. I hope someone can help me understand how I can get a valid "code" for this application.
BTW I don't want to use a code library in this situation, just php and CURL similar to the above.
Thanks
TimS

Related

Microsoft Azure Auth token

I ma having an issue with getting my access token. I know I have allowed access in the settings in my app. I can read my email with there text page:
I can use the token they provide and retrieve it. But I need to be able to get my own token of course. When I have code below it opens the login page and says "There was an issue looking up your account. Tap Next to try again."
Am I using the right API?
$curl = curl_init();
$auth_data = array(
"Content-Type: application/x-www-form-urlencoded",
'client_id' => 'my-client-id',
'response_type' => 'code',
'redirect_uri' => 'https://myurl.com/authorize.php',
'response_mode' => 'query',
'scope' => 'https://graph.microsoft.com/.default',
'state' => '12345'
);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, $auth_data);
curl_setopt($curl, CURLOPT_URL, "https://login.microsoftonline.com/{my_tenant_id}/oauth2/v2.0/authorize");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
$result = curl_exec($curl);
if(!$result){die("Connection Failure");}
curl_close($curl);
//echo $result;
var_dump($result);
//print_r($result);

Unable To Get Twitch API OAuth For PHP In 2022

I have been unable to get an OAuth token. I have spent about 4 hours trying. The various iterations and changes the Twitch API has been through is leaving me unsure and confused. The Twitch Developers now have a message posted stating V5 API being decommission on February 28th 2022. I am lost.
This is where I am at right now. The "code" below is from the Getting authorization code here.
<?php
function file_get_contents_curl($url) {
$curlHeader = [
'client_id' => '"CLIENT ID"',
'client_secret' => '"CLIENT SECRET"',
'code' => '"POST TOKEN"',
'grant_type' => '"AUTHENTICATION CODE"',
'redirect_uri' => 'https://rons-home.net'
];
$ch = curl_init();
curl_setopt($ch, CURLOPT_AUTOREFERER, TRUE);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($ch, CURLOPT_HTTPHEADER, $curlHeader);
$data = curl_exec($ch);
curl_close($ch);
return $data;
}
print_r( file_get_contents_curl( 'https://id.twitch.tv/oauth2/authorize' ) );
Response
{"status":400,"message":"missing response type"}
This is a valid curl call to do the Step 3 of user oAuth
Most notably:
you were trying to pass paramters as a header
And you have extra " littered in your paramerters
And your grant type if very wrong
Substitute the constants as needed.
$ch = curl_init('https://id.twitch.tv/oauth2/token');
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, array(
'client_id' => CLIENT_ID,
'client_secret' => CLIENT_SECRET,
'code' => $_GET['code'],
'grant_type' => 'authorization_code',
'redirect_uri' => REDIRECT_URI
));
// fetch the data
$r = curl_exec($ch);
// get the information about the result
$i = curl_getinfo($ch);
// close the request
curl_close($ch);

cURL PHP Newbie Needs a hand

I am completely new to cURL requests in PHP.
I have an API that gives me the info below and wants me to send a POST request via cURL. I've tried some basic cURL examples but have no idea what how the additional data should be sent.
API Docs contain the following:
curl https://api.23andme.com/token/
-d client_id=xxx \
-d client_secret=yyy \
-d grant_type=authorization_code \
-d code=zzz \
-d "redirect_uri=https://localhost:5000/receive_code/"
-d "scope=basic%20rs3094315"
Here is my example code:
$data = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" => "http://localhost/23andme/",
"scope" => "basic"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch); //Uncomment to make it live again
if (!$response)
{
return false;
}
echo json_decode($response);
You could try
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
With your data array will send that as POST data since you already have curl_setopt($ch, CURLOPT_POST, true);.
http://php.net/manual/en/function.curl-setopt.php
So
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
Cheers
You just missed two things in your code, here is a complete example using your code as base:
<?php
/* you must define an URL to POST to */
$url = "";
$data = array(
"client_id" => $client_id,
"client_secret" => $client_secret,
"grant_type" => "authorization_code",
"code" => $code,
"redirect_uri" => "http://localhost:8080/nope",
"scope" => "basic"
);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, $data);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true)
/* this line below was missing in your code */;
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
$response = curl_exec($ch);
if (!$response)
{
echo 'A error has occurred ' . curl_error($ch);
return false;
}
echo json_decode($response);
?>
Try and adjust to your needs.

instagram API responds APISubscriptionError Invalid Response

I have a few subscriptions. Time to time subscription ends and I start the ended subscription again.
Now, my most used subscriptions ended and I can't start again. I'm working in PHP. Nothing has changed in my code, but now I get error while trying to subscribe.
{"meta":{"error_type":"APISubscriptionError","code":400,"error_message":"Invalid response"}}
My other subscriptions are still active but 4 of them are failing. I couldn't fix it.
Edit - Code:
<?php
echo $_GET['hub_challenge'];
$client_id='XXXXX';
$client_secret='XXXXX';
$object = 'tag';
$aspect = 'media';
$object_id = 'XXXXX';
$verify_token='XXXXX';
$callback_url = 'http://XXXXX/callback.php';
$attachment = array(
'client_id' => $client_id,
'client_secret' => $client_secret,
'object' => $object,
'object_id' => $object_id,
'aspect' => $aspect,
'verify_token' => $verify_token,
'callback_url'=>$callback_url
);
$url = "https://api.instagram.com/v1/subscriptions/";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 2);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $attachment);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close ($ch);
print_r($result);
Instagram sends a GET request to uour callback_url and wants you to response with hub.challenge.

Make 2 cURL requets with $_POST

I am trying to do 2 requets in cURL. The first request is login me in, and I conserve a txt file to make the second requests when still loged.
The first part work like a charm, the second part doesn't work.
There is my code.
$lien = 'https://mysite.com/login';
$postfields = array(
'username' => 'test',
'password' => 'test'
);
$path_cookie = 'connexion.txt';
if (!file_exists(realpath($path_cookie))) touch($path_cookie);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien);
curl_setopt($curl, CURLOPT_COOKIESESSION, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields);
curl_setopt($curl, CURLOPT_COOKIEJAR, realpath($path_cookie));
$return = curl_exec($curl);
echo($return);
curl_close($curl);
$lien2 = 'https://mysite.com/myform';
$postfields2 = array(
'data1' => 'test123',
'data2' => 'Account',
'sort' => '3',
'fileFormat' => '0',
'timezone' => 'Eastern+Standard+Time',
'zeroRated' => 'true',
'startDate' => '2013-05-01',
'endDate' => '2013-05-31'
);
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $lien2);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $postfields2);
curl_setopt($curl, CURLOPT_COOKIEFILE, realpath($path_cookie));
$return = curl_exec($curl);
echo $return;
curl_close($curl);
unlink(realpath($path_cookie));
I am using the second part to $_post data to execute a form. The form processing takes place in the same page...
My connexion still on, but I don't know, the server is shooting me an error like :
An exception occurred while processing your request. We recorded the exception..
It doesn't help me. Any one have an idea?
Thanks.
You have maybe a problem with SSL at this point.
Try to add these options:
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
finally I receive an answer from the support team. The "Eastern+Standard+Time" should be "Eastern Standard Time", the server will automaticly escape those string ... Thaks to every body for your help !

Categories