Created
March 5, 2015 20:20
-
-
Save ydn/043bd44bfe5fe8b0c1be to your computer and use it in GitHub Desktop.
Gemini API
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/* Example code to access Gemini API: Fetch advertiser information, create a new campaign and read specific campaign data | |
Prerequisites: | |
1. Sign up for an account on https://admanager.yahoo.com | |
2. Download YahooOAuth2.class.php file from here: https://github.com/saurabhsahni/php-yahoo-oauth2/blob/master/YahooOAuth2.class.php | |
3. PHP modules for json_decode & curl | |
4. A webserver running this code on port 80/443. Yahoo OAuth callback is only supported on these ports | |
*/ | |
require "YahooOAuth2.class.php"; | |
#Your Yahoo API consumer key & secret with access to Gemini data | |
define("CONSUMER_KEY","<your consumer key>"); | |
define("CONSUMER_SECRET","<your consumer secret>"); | |
$redirect_uri="http://".$_SERVER['SERVER_NAME'] . $_SERVER['PHP_SELF'];//Or your other redirect URL - must match the callback domain | |
$gemini_api_endpoint="https://api.admanager.yahoo.com/v1/rest"; | |
$oauth2client=new YahooOAuth2(); | |
if (isset($_GET['code'])){ | |
$code=$_GET['code']; | |
} | |
else { | |
$code=0; | |
} | |
if($code){ | |
#oAuth 3-legged authorization is successful, fetch access token | |
$token=$oauth2client->get_access_token(CONSUMER_KEY,CONSUMER_SECRET,$redirect_uri,$code); | |
#Access token is available. Do API calls. | |
$headers= array('Authorization: Bearer '.$token,'Accept: application/json','Content-Type: application/json'); | |
#Fetch Advertiser Name and Advertiser ID | |
$url=$gemini_api_endpoint."/advertiser/"; | |
$resp=$oauth2client->fetch($url,$postdata="",$auth="",$headers); | |
$jsonResponse = json_decode( $resp); | |
$advertiserName = $jsonResponse->response[0]->advertiserName; | |
$advertiserId = $jsonResponse->response[0]->id; | |
echo "Welcome ".$advertiserName; | |
} | |
else { | |
# no valid access token available, go to authorization server | |
header("HTTP/1.1 302 Found"); | |
header("Location: " . $oauth2client->getAuthorizationURL(CONSUMER_KEY,$redirect_uri)); | |
exit; | |
} | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment