I am trying to access accounts that are not linked to our MCC by the consent of the user.
It first starts here,
$user = new AdWordsUser();
$user->SetOAuth2Info(array(
"response_type" => 'code',
"client_id" => $clientId,
"client_secret" => $clientSecret,
"access_token" => $access,
"refresh_token" => $refresh,
"scope" => 'offline'
));
$params = array(
"response_type" => 'code',
"client_id" => $clientId,
"client_secret" => $clientSecret,
"access_token" => $access,
"refresh_token" => $refresh,
"scope" => 'offline'
);
/*
* public function GetAuthorizationUrl(array $credentials,
$redirectUri = NULL, $offline = NULL, array $params = NULL) {
*/
// Generate an authorization URL given the callback URL
try{
$OAuth2Handler = $user->GetOAuth2Handler();
print_r( $OAuth2Handler );
$authUrl = $OAuth2Handler->GetAuthorizationUrl($params, $callbackUrl, true, null);
}catch( Exception $e ){
print_r($e->getMessage());
}
header("Location: $authUrl");
Then it takes you to the consent page.
After login is successful,
Not sure if I need to set scope = 'offline' Paul Matthews said something about that but wasnt clear. There is another scope but it is a URL so I dont know what hes talking about. Maybe you can shed some light on that.
Anyway, the callback url runs this code,
$user = new AdWordsUser();
$user->SetOAuth2Info(array(
"response_type" => 'code',
"client_id" => $clientId,
"client_secret" => $clientSecret,
"access_token" => $access,
"refresh_token" => $refresh,
"scope" => 'offline'
));
$authCode = $_REQUEST["code"];
//GetOAuth2Credential function copied below
$oauth2Info = $this->Google_model->GetOAuth2Credential($user, $authCode, $callbackUrl);
$user->SetOAuth2Info($oauth2Info);
$user->SetAuthToken( $authCode );
function GetOAuth2Credential($user, $code, $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: ";
// 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();
}
I still get the alert, [AuthorizationError.USER_PERMISSION_DENIED @ ; trigger:'']