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;
}
@PostMapping(value = "/link/account", consumes = "application/json; charset=UTF-8")
public String link(@RequestBody GoogleSignInObject googleSignInObject){
return manageAccount.linkAccount(googleSignInObject);
}
<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", headers: { 'Authorization' : 'Bearer authorized-code' }, contentType: 'application/json', success: function(result) { }, processData: false, data: JSON.stringify(googleSignInObject)
}); } else { }}</script></body></html>