本文整理汇总了PHP中AdWordsUser::GetOAuth2Handler方法的典型用法代码示例。如果您正苦于以下问题:PHP AdWordsUser::GetOAuth2Handler方法的具体用法?PHP AdWordsUser::GetOAuth2Handler怎么用?PHP AdWordsUser::GetOAuth2Handler使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类AdWordsUser
的用法示例。
在下文中一共展示了AdWordsUser::GetOAuth2Handler方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的PHP代码示例。
示例1: GetOAuth2Credential
/**
* Gets an OAuth2 credential.
* @param AdWordsUser $user the user that contains the client ID and secret
* @return array the user's OAuth 2 credentials
*/
function GetOAuth2Credential(AdWordsUser $user)
{
$redirectUri = NULL;
$offline = TRUE;
// 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.
$OAuth2Handler = $user->GetOAuth2Handler();
$authorizationUrl = $OAuth2Handler->GetAuthorizationUrl($user->GetOAuth2Info(), $redirectUri, $offline);
// 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:\n%s\n\n", $authorizationUrl);
print "After approving the token enter the authorization code here: ";
$stdin = fopen('php://stdin', 'r');
$code = trim(fgets($stdin));
fclose($stdin);
print "\n";
// Get the access token using the authorization code. Ensure you use the same
// redirect URL used when requesting authorization.
$user->SetOAuth2Info($OAuth2Handler->GetAccessToken($user->GetOAuth2Info(), $code, $redirectUri));
// The access token expires but the refresh token obtained for offline use
// doesn't, and should be stored for later use.
return $user->GetOAuth2Info();
}
示例2: GetHeaders
/**
* Gets the HTTP headers for the report download request.
* @param AdWordsUser $user the AdWordsUser to get credentials from
* @param string $url the URL the request will be made to
* @param array $options the options for the download
* @return array and array of strings, which are header names and values
*/
private static function GetHeaders($user, $url, array $options = NULL)
{
$headers = array();
$version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion();
// Authorization.
$authHeader = NULL;
$oAuth2Info = $user->GetOAuth2Info();
$oAuth2Handler = $user->GetOAuth2Handler();
if (!empty($oAuth2Info)) {
$oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info);
$user->SetOAuth2Info($oAuth2Info);
$authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info);
} else {
DeprecationUtils::CheckUsingClientLoginWithUnsupportedVersion($user, AdWordsUser::FINAL_CLIENT_LOGIN_VERSION, $version);
$authHeader = sprintf(self::CLIENT_LOGIN_FORMAT, $user->GetAuthToken());
}
$headers['Authorization'] = $authHeader;
// Developer token.
$headers['developerToken'] = $user->GetDeveloperToken();
// Target client.
$email = $user->GetEmail();
$clientCustomerId = $user->GetClientCustomerId();
if (isset($clientCustomerId)) {
$headers['clientCustomerId'] = $clientCustomerId;
} else {
if ($version < 'v201109' && isset($email)) {
$headers['clientEmail'] = $email;
} else {
throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.');
}
}
// Flags.
if (isset($options['returnMoneyInMicros'])) {
DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion(self::FINAL_RETURN_MONEY_IN_MICROS_VERSION, $version);
$headers['returnMoneyInMicros'] = $options['returnMoneyInMicros'] ? 'true' : 'false';
}
return $headers;
}
示例3: GetHeaders
/**
* Gets the HTTP headers for the report download request.
* @param AdWordsUser $user the AdWordsUser to get credentials from
* @param string $url the URL the request will be made to
* @param array $options the options for the download
* @return array and array of strings, which are header names and values
*/
private static function GetHeaders($user, $url, array $options = NULL)
{
$headers = array();
$version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion();
// Authorization.
$authHeader = NULL;
$oAuth2Info = $user->GetOAuth2Info();
$oAuth2Handler = $user->GetOAuth2Handler();
if (!empty($oAuth2Info)) {
$oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info);
$user->SetOAuth2Info($oAuth2Info);
$authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info);
} else {
throw new ServiceException('OAuth 2.0 authentication is required.');
}
$headers['Authorization'] = $authHeader;
// Developer token.
$headers['developerToken'] = $user->GetDeveloperToken();
// Target client.
$clientCustomerId = $user->GetClientCustomerId();
if (isset($clientCustomerId)) {
$headers['clientCustomerId'] = $clientCustomerId;
} else {
throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.');
}
// Flags.
if (isset($options['returnMoneyInMicros'])) {
DeprecationUtils::CheckUsingReturnMoneyInMicrosWithUnsupportedVersion(self::FINAL_RETURN_MONEY_IN_MICROS_VERSION, $version);
$headers['returnMoneyInMicros'] = $options['returnMoneyInMicros'] ? 'true' : 'false';
}
return $headers;
}
示例4: GetHeaders
/**
* Gets the HTTP headers for the report download request.
* @param AdWordsUser $user the AdWordsUser to get credentials from
* @param string $url the URL the request will be made to
* @param array $options the options for the download
* @return array and array of strings, which are header names and values
*/
private static function GetHeaders($user, $url, array $options = null)
{
$headers = array();
$version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion();
// Authorization.
$authHeader = null;
$oAuth2Info = $user->GetOAuth2Info();
$oAuth2Handler = $user->GetOAuth2Handler();
if (!empty($oAuth2Info)) {
$oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info);
$user->SetOAuth2Info($oAuth2Info);
$authHeader = $oAuth2Handler->FormatCredentialsForHeader($oAuth2Info);
} else {
throw new ServiceException('OAuth 2.0 authentication is required.');
}
$headers['Authorization'] = $authHeader;
// Developer token.
$headers['developerToken'] = $user->GetDeveloperToken();
// Target client.
$clientCustomerId = $user->GetClientCustomerId();
if (isset($clientCustomerId)) {
$headers['clientCustomerId'] = $clientCustomerId;
} else {
throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.');
}
// Flags.
if (isset($options['skipReportHeader'])) {
$headers['skipReportHeader'] = $options['skipReportHeader'] ? 'true' : 'false';
}
if (isset($options['skipColumnHeader'])) {
$headers['skipColumnHeader'] = $options['skipColumnHeader'] ? 'true' : 'false';
}
if (isset($options['skipReportSummary'])) {
$headers['skipReportSummary'] = $options['skipReportSummary'] ? 'true' : 'false';
}
if (isset($options['includeZeroImpressions'])) {
$headers['includeZeroImpressions'] = $options['includeZeroImpressions'] ? 'true' : 'false';
}
return $headers;
}
示例5: GetHeaders
/**
* Gets the HTTP headers for the report download request.
* @param AdWordsUser $user the AdWordsUser to get credentials from
* @param string $url the URL the request will be made to
* @param array $options the options for the download
* @return array and array of strings, which are header names and values
*/
private static function GetHeaders($user, $url, array $options = NULL)
{
$headers = array();
$version = !empty($options['version']) ? $options['version'] : $user->GetDefaultVersion();
// Authorization.
if ($user->GetOAuthInfo()) {
$oauthParams = $user->GetOAuthHandler()->GetSignedRequestParameters($user->GetOAuthInfo(), $url, 'POST');
$headers['Authorization'] = 'OAuth ' . $user->GetOAuthHandler()->FormatParametersForHeader($oauthParams);
} elseif ($user->GetOAuth2Info()) {
if (!$user->IsOAuth2AccessTokenValid() && $user->CanRefreshOAuth2AccessToken()) {
$user->RefreshOAuth2AccessToken();
}
$oauth2Header = $user->GetOAuth2Handler()->FormatCredentialsForHeader($user->GetOAuth2Info());
$headers['Authorization'] = $oauth2Header;
} else {
$headers['Authorization'] = 'GoogleLogin auth=' . $user->GetAuthToken();
}
// Developer token.
$headers['developerToken'] = $user->GetDeveloperToken();
// Target client.
$email = $user->GetEmail();
$clientId = $user->GetClientId();
if (isset($clientId)) {
if (strpos($clientId, '@') !== FALSE) {
if ($version < 'v201109') {
$headers['clientEmail'] = $clientId;
} else {
throw new ReportDownloadException('Client emails are not supported ' . 'in versions v201109 and later.');
}
} else {
$headers['clientCustomerId'] = $clientId;
}
} else {
if ($version < 'v201109' && isset($email)) {
$headers['clientEmail'] = $email;
} else {
throw new ReportDownloadException('The client customer ID must be ' . 'specified for report downloads.');
}
}
// Flags.
if (isset($options['returnMoneyInMicros'])) {
$headers['returnMoneyInMicros'] = $options['returnMoneyInMicros'] ? 'true' : 'false';
}
return $headers;
}
示例6: AdWordsUser
// AddSitelinks example, except you can also filter based on the business
// name and category of each FeedItem by using a FeedAttributeOperand in
// your matching function.
// OPTIONAL: Create an AdGroupFeed for even more fine grained control over
// which feed items are used at the AdGroup level.
}
// Don't run the example if the file is being included.
if (__FILE__ != realpath($_SERVER['PHP_SELF'])) {
return;
}
try {
// Get AdWordsUser from credentials in "../auth.ini"
// relative to the AdWordsUser.php file's directory.
$user = new AdWordsUser();
$oAuth2Info = $user->GetOAuth2Info();
$oAuth2Handler = $user->GetOAuth2Handler();
if (!isset($oAuth2Info['access_token'])) {
$oAuth2Info = $oAuth2Handler->GetOrRefreshAccessToken($oAuth2Info);
$user->SetOAuth2Info($oAuth2Info);
}
// Log every SOAP XML request and response.
$user->LogAll();
// If the GMB_EMAIL_ADDRESS is the same user you used to generate your
// AdWords API refresh token, leave the assignment below unchanged.
// Otherwise, to obtain an access token for your GMB account, run the
// Auth/GetRefreshToken example. Make sure you are logged in as the same user
// as GMB_EMAIL_ADDRESS above when you follow the link provided by the example
// then call GetOAuth2Info on the generated AdWordsUser object and copy and
// paste the value into the assignment below.
define('GMB_ACCESS_TOKEN', $oAuth2Info['access_token']);
// Run the example.