本文整理汇总了PHP中AdWordsUser::GetOAuth2AccessToken方法的典型用法代码示例。如果您正苦于以下问题:PHP AdWordsUser::GetOAuth2AccessToken方法的具体用法?PHP AdWordsUser::GetOAuth2AccessToken怎么用?PHP AdWordsUser::GetOAuth2AccessToken使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AdWordsUser
的用法示例。
在下文中一共展示了AdWordsUser::GetOAuth2AccessToken方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: UseOAuth2Example
/**
* Runs the example.
* @param string $clientId the OAuth2 client ID
* @param string $clientSecret the OAuth2 client secret
*/
function UseOAuth2Example($clientId, $clientSecret)
{
// Set the OAuth2 client ID and secret.
$oauth2Info = array('client_id' => $clientId, 'client_secret' => $clientSecret);
// Create the AdWordsUser and set the OAuth2 info.
$user = new AdWordsUser();
$user->SetOAuth2Info($oauth2Info);
$user->LogAll();
// Get the authorization URL for the OAuth2 token.
// No redirect URL is being used since this is an installed application. A web
// application would pass in a redirect URL back to the application,
// ensuring it's one that has been configured in the API console.
// Passing true for the second parameter ($offline) will provide us a refresh
// token which can used be refresh the access token when it expires.
$authorizationUrl = $user->GetOAuth2AuthorizationUrl(NULL, TRUE);
// In a web application you would redirect the user to the authorization URL
// and after approving the token they would be redirected back to the
// redirect URL, with the URL parameter "code" added. For desktop
// or server applications, spawn a browser to the URL and then have the user
// enter the authorization code that is displayed.
printf("Log in to your AdWords account and open the following URL: %s\n", $authorizationUrl);
print 'After approving the token enter the authorization code here: ';
$stdin = fopen('php://stdin', 'r');
$code = trim(fgets($stdin));
fclose($stdin);
// Get the access token using the authorization code. Ensure you use the same
// redirect URL used when requesting authorization.
$user->GetOAuth2AccessToken($code, NULL);
// The access token expires but the refresh token obtained for offline use
// doesn't, and should be stored for later use.
$oauth2Info = $user->GetOAuth2Info();
print "OAuth2 authorization successful.\n";
print_r($oauth2Info);
// Get the number of campaigns in the account.
$campaignService = $user->GetService('CampaignService', ADWORDS_VERSION);
$selector = new Selector();
$selector->fields = array('Id');
$selector->paging = new Paging(0, 0);
$page = $campaignService->get($selector);
// Display number of campaigns.
printf("Found %d campaigns.\n", $page->totalNumEntries);
}