Skip to content

Commit 8661c59

Browse files
author
Saurabh Sahni
committed
oAuth2 & gemini API sample code
1 parent 004b3cd commit 8661c59

File tree

2 files changed

+146
-0
lines changed

2 files changed

+146
-0
lines changed

YahooOAuth2.class.php

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
/*
3+
Example class to access Yahoo OAuth2 protected APIs, based on https://developer.yahoo.com/oauth2/guide/
4+
Find documentation and support on Yahoo Developer Network: https://developer.yahoo.com/forums
5+
6+
*/
7+
8+
class YahooOAuth2
9+
{
10+
11+
const AUTHORIZATION_ENDPOINT = 'https://api.login.yahoo.com/oauth2/request_auth';
12+
const TOKEN_ENDPOINT = 'https://api.login.yahoo.com/oauth2/get_token';
13+
14+
public function fetch($url,$postdata="",$auth="",$headers="")
15+
{
16+
$curl = curl_init($url);
17+
if($postdata) {
18+
curl_setopt($curl, CURLOPT_POST, true);
19+
curl_setopt($curl, CURLOPT_POSTFIELDS, $postdata);
20+
} else {
21+
curl_setopt($curl, CURLOPT_POST, false);
22+
}
23+
if($auth){
24+
curl_setopt($curl, CURLOPT_USERPWD, $auth);
25+
}
26+
if($headers){
27+
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
28+
}
29+
curl_setopt($curl, CURLOPT_HEADER, false);
30+
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
31+
$response = curl_exec( $curl );
32+
if (empty($response)) {
33+
// some kind of an error happened
34+
die(curl_error($curl));
35+
curl_close($curl); // close cURL handler
36+
} else {
37+
$info = curl_getinfo($curl);
38+
curl_close($curl); // close cURL handler
39+
if($info['http_code'] != 200 && $info['http_code'] != 201 ) {
40+
echo "Received error: " . $info['http_code']. "\n";
41+
echo "Raw response:".$response."\n";
42+
die();
43+
}
44+
}
45+
return $response;
46+
}
47+
public function getAuthorizationURL($client_id,$redirect_uri,$language="en-us")
48+
{
49+
$url = self::AUTHORIZATION_ENDPOINT;
50+
$authorization_url=$url.'?'.'client_id='.$client_id.'&redirect_uri='.$redirect_uri.'&language='.$language.'&response_type=code';
51+
return $authorization_url;
52+
}
53+
54+
55+
public function get_access_token($clientId, $clientSecret,$redirect_uri,$code) {
56+
$url=self::TOKEN_ENDPOINT;
57+
$postdata=array("redirect_uri"=>$redirect_uri,"code"=>$code,"grant_type"=>"authorization_code");
58+
$auth=$clientId . ":" . $clientSecret;
59+
$response=self::fetch($url,$postdata,$auth);
60+
61+
// Convert the result from JSON format to a PHP array
62+
$jsonResponse = json_decode( $response );
63+
return $jsonResponse->access_token;
64+
}
65+
66+
}
67+
68+
?>

index.php

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
<?php
2+
/* Example code to access Gemini API: Fetch advertiser information, create a new campaign and read specific campaign data*/
3+
4+
require "YahooOAuth2.class.php"; #Download here: https://github.com/saurabhsahni/php-yahoo-oauth2/
5+
6+
/*Your Yahoo API consumer key & secret with access to Gemini data */
7+
8+
define("CONSUMER_KEY","<your consumer key>");
9+
define("CONSUMER_SECRET","<your consumer secret>");
10+
$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];//Or your other redirect URL - must match the callback domain
11+
12+
$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest";
13+
14+
$oauth2client=new YahooOAuth2();
15+
16+
if (isset($_GET['code'])){
17+
$code=$_GET['code'];
18+
}
19+
else {
20+
$code=0;
21+
}
22+
23+
if($code){
24+
#oAuth 3-legged authorization is successful, fetch access token
25+
$token=$oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code);
26+
27+
#access token is available. Do API calls.
28+
29+
$headers= array(
30+
'Authorization: Bearer '.$token,
31+
'Accept: application/json',
32+
'Content-Type: application/json'
33+
);
34+
35+
//Fetch Advertiser Name and Advertiser ID
36+
$url=$gemini_api_endpoint."/advertiser/";
37+
38+
$resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers);
39+
$jsonResponse = json_decode( $resp);
40+
$advertiserName = $jsonResponse->response[0]->advertiserName;
41+
$advertiserId = $jsonResponse->response[0]->id;
42+
echo "Welcome ".$advertiserName;
43+
44+
//Create a new campaign
45+
$url=$gemini_api_endpoint."/campaign";
46+
$postdata='{
47+
"status":"PAUSED",
48+
"campaignName":"NativeAdsCampaign",
49+
"budget": 3000,
50+
"budgetType": "LIFETIME",
51+
"advertiserId": '.$advertiserId.',
52+
"channel":"NATIVE"
53+
}';
54+
55+
$resp=$oauth2client->fetch($url,$postdata=$postdata,$auth="",$headers);
56+
$jsonResponse = json_decode( $resp);
57+
58+
$campaignID=$jsonResponse->response->id;
59+
$campaignName=$jsonResponse->response->campaignName;
60+
61+
echo "\n<br>Created a new campaign with ID: ".$campaignID;
62+
63+
//Read specific campaign data
64+
$url=$gemini_api_endpoint."/campaign/".$campaignID;
65+
$resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers);
66+
$jsonResponse = json_decode( $resp);
67+
echo "\n<br> Campaign object:<br>\n";
68+
print_r($jsonResponse->response);
69+
}
70+
else {
71+
/* no valid access token available, go to authorization server */
72+
header("HTTP/1.1 302 Found");
73+
header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri));
74+
exit;
75+
}
76+
77+
?>
78+

0 commit comments

Comments
 (0)