Hello,
I'm got AuthenticationError.OAUTH_TOKEN_INVALID when try to link adwords
account to manager adwords account
here my code at server:
private void sendRequest(ManagedCustomerServiceInterface
managedCustomerService, Long customerId, Long managerId) throws RemoteException
{
//send email request
LinkOperation linkOp = new LinkOperation();
ManagedCustomerLink link = new ManagedCustomerLink();
link.setClientCustomerId(customerId); //customer id
link.setLinkStatus(LinkStatus.PENDING);
link.setManagerCustomerId(managerId);//manager id
linkOp.setOperand(link);
linkOp.setOperator(Operator.ADD);
managedCustomerService.mutateLink(new LinkOperation[]{linkOp});
log.info("request has been sent!");
}
private void acceptRequest(ManagedCustomerServiceInterface
managedCustomerService, Long customerId, Long managerId) throws RemoteException
{
//accept
LinkOperation linkOps = new LinkOperation();
ManagedCustomerLink links = new ManagedCustomerLink();
links.setClientCustomerId(customerId); //customer id
links.setLinkStatus(LinkStatus.ACTIVE);
links.setManagerCustomerId(managerId);//manager id
linkOps.setOperand(links);
linkOps.setOperator(Operator.SET);
managedCustomerService.mutateLink(new LinkOperation[]{linkOps});
log.info("request has been accepted!");
}
private AdWordsSession getSessionByAccessToken(String accessToken) {
AdWordsSession session = null;
try {
GoogleCredential credential = new
GoogleCredential().setAccessToken(accessToken);
// Construct an AdWordsSession.
session = new AdWordsSession.Builder()
.withDeveloperToken(developerToken)
.withUserAgent("Knorex-adwords")
.withOAuth2Credential(credential)
.build();
} catch (Exception e) {
log.error(e.getMessage());
}
return session;
}
private Long parseLong(String str){
str = str.replaceAll("-","");
return Long.parseLong(str);
}
public String linkAccount(GoogleSignInObject googleSignInObject){
AdWordsSession session = getSession();
AdWordsServicesInterface adWordsServices = AdWordsServices.getInstance();
ManagedCustomerServiceInterface managedCustomerService =
adWordsServices.get(session, ManagedCustomerServiceInterface.class);
Long cusId = parseLong(googleSignInObject.getCustomerId());
Long manId = parseLong(clientCustomer);
try {
sendRequest(managedCustomerService, cusId, manId);
} catch (ApiException e){
log.error(e.getMessage());
} catch (RemoteException e) {
log.error(e.getMessage());
return FAIL;
}
//change session
GoogleTokenResponse tokenResponse;
try {
tokenResponse = new GoogleAuthorizationCodeTokenRequest(
new NetHttpTransport(),
JacksonFactory.getDefaultInstance(),
"https://www.googleapis.com/oauth2/v4/token",
googleSignInObject.getClientId(),
googleSignInObject.getClientSecret(),
googleSignInObject.getAuthCode(),
googleSignInObject.getRedirectUri()) // Specify the same
redirect URI that you use with your web
// app. If you don't have a web version of your app, you can
// specify an empty string.
.execute();
} catch (IOException e) {
log.error(e.getMessage());
return FAIL;
}
String accessToken = tokenResponse.getAccessToken();
session = getSessionByAccessToken(accessToken);
session.setClientCustomerId(googleSignInObject.getCustomerId());
ManagedCustomerServiceInterface managedCustomerService2 =
adWordsServices.get(session, ManagedCustomerServiceInterface.class);
try {
acceptRequest(managedCustomerService2, cusId, manId); //i get error here
} catch (RemoteException e) {
log.error(e.getMessage());
return FAIL;
}
return SUCCESS;
}
controller:
@PostMapping(value = "/link/account", consumes = "application/json;
charset=UTF-8")
public String link(@RequestBody GoogleSignInObject googleSignInObject){
return manageAccount.linkAccount(googleSignInObject);
}
font-end:
<html itemscope itemtype="http://schema.org/Article">
<head>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js">
</script>
<script src="https://apis.google.com/js/client:platform.js?onload=start"
async defer>
</script>
<script>
function start() {
gapi.load('auth2', function() {
auth2 = gapi.auth2.init({
client_id: 'my clientId',
// Scopes to request in addition to 'profile' and 'email'
});
});
}
</script>
</head>
<body>
<button id="signinButton">Sign in with Google</button>
<script>
$('#signinButton').click(function() {
auth2.grantOfflineAccess().then(signInCallback);
});
function signInCallback(authResult) {
if (authResult['code']) {
$('#signinButton').attr('style', 'display: none');
var googleSignInObject = {
authCode: authResult['code'],
clientId: 'my client id,
clientSecret: 'my client secret',
customerId: 'my customer id',
redirectUri: 'http://localhost:9000'
};
$.ajax({
type: 'POST',
dataType: "json",
url: 'http://localhost:8081/api/account/link/account',
headers: {
'Authorization' : 'Bearer authorized-code'
},
contentType: 'application/json',
success: function(result) {
},
processData: false,
data: JSON.stringify(googleSignInObject)
});
} else {
}
}
</script>
</body>
</html>
i got error when try to accept request
pleas help!
--
--
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
Also find us on our blog:
https://googleadsdeveloper.blogspot.com/
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
You received this message because you are subscribed to the Google
Groups "AdWords API and Google Ads API Forum" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/adwords-api?hl=en
---
You received this message because you are subscribed to the Google Groups
"AdWords API and Google Ads API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
Visit this group at https://groups.google.com/group/adwords-api.
To view this discussion on the web visit
https://groups.google.com/d/msgid/adwords-api/fabdf77a-60f8-4d8a-87d7-c059f46a0359%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.