|
| 1 | +// Copyright 2026 Google LLC |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// https://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package com.google.ads.googleads.examples.advancedoperations; |
| 16 | + |
| 17 | +import com.beust.jcommander.Parameter; |
| 18 | +import com.google.ads.googleads.examples.utils.ArgumentNames; |
| 19 | +import com.google.ads.googleads.examples.utils.CodeSampleParams; |
| 20 | +import com.google.ads.googleads.lib.GoogleAdsClient; |
| 21 | +import com.google.ads.googleads.v24.enums.MultiPartyAuthOperationTypeEnum.MultiPartyAuthOperationType; |
| 22 | +import com.google.ads.googleads.v24.enums.MultiPartyAuthReviewStatusEnum.MultiPartyAuthReviewStatus; |
| 23 | +import com.google.ads.googleads.v24.enums.MultiPartyAuthReviewTargetResourceEnum.MultiPartyAuthReviewTargetResource; |
| 24 | +import com.google.ads.googleads.v24.errors.GoogleAdsError; |
| 25 | +import com.google.ads.googleads.v24.errors.GoogleAdsException; |
| 26 | +import com.google.ads.googleads.v24.errors.GoogleAdsFailure; |
| 27 | +import com.google.ads.googleads.v24.resources.CustomerUserAccessInvitation; |
| 28 | +import com.google.ads.googleads.v24.resources.CustomerUserAccessInvitationReview; |
| 29 | +import com.google.ads.googleads.v24.resources.CustomerUserAccessReview; |
| 30 | +import com.google.ads.googleads.v24.resources.MultiPartyAuthReview; |
| 31 | +import com.google.ads.googleads.v24.services.GoogleAdsRow; |
| 32 | +import com.google.ads.googleads.v24.services.GoogleAdsServiceClient; |
| 33 | +import com.google.ads.googleads.v24.services.GoogleAdsServiceClient.SearchPagedResponse; |
| 34 | +import com.google.ads.googleads.v24.services.MultiPartyAuthReviewServiceClient; |
| 35 | +import com.google.ads.googleads.v24.services.ResolveMultiPartyAuthReviewOperation; |
| 36 | +import com.google.ads.googleads.v24.services.ResolveMultiPartyAuthReviewResponse; |
| 37 | +import com.google.ads.googleads.v24.services.ResolveMultiPartyAuthReviewResultOrError; |
| 38 | +import com.google.ads.googleads.v24.utils.ErrorUtils; |
| 39 | +import java.io.FileNotFoundException; |
| 40 | +import java.io.IOException; |
| 41 | +import java.util.ArrayList; |
| 42 | +import java.util.Collections; |
| 43 | +import java.util.List; |
| 44 | + |
| 45 | +/** |
| 46 | + * Fetches pending multi-party auth reviews for a customer account and approves them. |
| 47 | + */ |
| 48 | +public class FetchAndApprovePendingMultiPartyAuthReviews { |
| 49 | + |
| 50 | + private static class FetchAndApprovePendingMultiPartyAuthReviewsParams extends CodeSampleParams { |
| 51 | + |
| 52 | + @Parameter(names = ArgumentNames.CUSTOMER_ID, required = true) |
| 53 | + private Long customerId; |
| 54 | + } |
| 55 | + |
| 56 | + public static void main(String[] args) { |
| 57 | + FetchAndApprovePendingMultiPartyAuthReviewsParams params = |
| 58 | + new FetchAndApprovePendingMultiPartyAuthReviewsParams(); |
| 59 | + if (!params.parseArguments(args)) { |
| 60 | + params.customerId = Long.parseLong("INSERT_CUSTOMER_ID_HERE"); |
| 61 | + } |
| 62 | + |
| 63 | + GoogleAdsClient googleAdsClient = null; |
| 64 | + try { |
| 65 | + googleAdsClient = GoogleAdsClient.newBuilder().fromPropertiesFile().build(); |
| 66 | + } catch (FileNotFoundException fnfe) { |
| 67 | + System.err.printf( |
| 68 | + "Failed to load GoogleAdsClient configuration from file. Exception: %s%n", fnfe); |
| 69 | + System.exit(1); |
| 70 | + } catch (IOException ioe) { |
| 71 | + System.err.printf("Failed to create GoogleAdsClient. Exception: %s%n", ioe); |
| 72 | + System.exit(1); |
| 73 | + } |
| 74 | + |
| 75 | + try { |
| 76 | + new FetchAndApprovePendingMultiPartyAuthReviews().runExample(googleAdsClient, params.customerId); |
| 77 | + } catch (GoogleAdsException gae) { |
| 78 | + System.err.printf( |
| 79 | + "Request ID %s failed due to GoogleAdsException. Underlying errors:%n", |
| 80 | + gae.getRequestId()); |
| 81 | + int i = 0; |
| 82 | + for (GoogleAdsError googleAdsError : gae.getGoogleAdsFailure().getErrorsList()) { |
| 83 | + System.err.printf(" Error %d: %s%n", i++, googleAdsError); |
| 84 | + } |
| 85 | + System.exit(1); |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Runs the example. |
| 91 | + * |
| 92 | + * @param client The Google Ads client. |
| 93 | + * @param customerId The Google Ads customer ID. |
| 94 | + */ |
| 95 | + private void runExample(GoogleAdsClient client, long customerId) { |
| 96 | + List<String> pendingReviews = fetchPendingMPAReviews(client, customerId); |
| 97 | + |
| 98 | + if (!pendingReviews.isEmpty()) { |
| 99 | + // Multi party auth reviews can only be resolved one at a time. In this code |
| 100 | + // example, we illustrate approving the first pending review request. |
| 101 | + approveMPAReview(client, customerId, pendingReviews.get(0)); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + // [START approve_mpa_review] |
| 106 | + /** |
| 107 | + * Approves the MPA auth review. |
| 108 | + * |
| 109 | + * @param client The Google Ads client. |
| 110 | + * @param customerId The Google Ads customer ID. |
| 111 | + * @param pendingReview The pending multi-party auth review resource name to be approved. |
| 112 | + */ |
| 113 | + private void approveMPAReview(GoogleAdsClient client, long customerId, String pendingReview) { |
| 114 | + // Currently, you can only approve one request at a time. In addition, the approvals |
| 115 | + // can only be done by a second administrator. |
| 116 | + ResolveMultiPartyAuthReviewOperation operation = |
| 117 | + ResolveMultiPartyAuthReviewOperation.newBuilder() |
| 118 | + .setMultiPartyAuthReview(pendingReview) |
| 119 | + .setNewStatus(MultiPartyAuthReviewStatus.APPROVED) |
| 120 | + .build(); |
| 121 | + |
| 122 | + try (MultiPartyAuthReviewServiceClient mpaReviewService = |
| 123 | + client.getLatestVersion().createMultiPartyAuthReviewServiceClient()) { |
| 124 | + ResolveMultiPartyAuthReviewResponse response = |
| 125 | + mpaReviewService.resolveMultiPartyAuthReview( |
| 126 | + String.valueOf(customerId), Collections.singletonList(operation)); |
| 127 | + |
| 128 | + ResolveMultiPartyAuthReviewResultOrError resultOrError = response.getResultOrError(0); |
| 129 | + if (resultOrError.hasResult()) { |
| 130 | + System.out.printf( |
| 131 | + "Approved multi-party auth review: %s.%n", |
| 132 | + resultOrError.getResult().getMultiPartyAuthReview()); |
| 133 | + if (resultOrError.getResult().hasCustomerUserAccessInvitation()) { |
| 134 | + System.out.printf( |
| 135 | + "New user invitation created: %s%n", |
| 136 | + resultOrError.getResult().getCustomerUserAccessInvitation()); |
| 137 | + } else if (resultOrError.getResult().hasCustomerUserAccess()) { |
| 138 | + System.out.printf( |
| 139 | + "Affected customer user access resource: %s%n", |
| 140 | + resultOrError.getResult().getCustomerUserAccess()); |
| 141 | + } |
| 142 | + } else if (resultOrError.hasPartialFailureError()) { |
| 143 | + GoogleAdsFailure failure = |
| 144 | + ErrorUtils.getInstance().getGoogleAdsFailure(resultOrError.getPartialFailureError()); |
| 145 | + System.out.printf( |
| 146 | + "%d partial failure error(s) occurred%n", |
| 147 | + failure.getErrorsCount()); |
| 148 | + } |
| 149 | + } |
| 150 | + } |
| 151 | + // [END approve_mpa_review] |
| 152 | + |
| 153 | + // [START fetch_mpa_review] |
| 154 | + /** |
| 155 | + * Fetches the pending MPA reviews. |
| 156 | + * |
| 157 | + * @param client The Google Ads client. |
| 158 | + * @param customerId The Google Ads customer ID. |
| 159 | + * @return A list of resource names for the pending multi-party auth reviews. |
| 160 | + */ |
| 161 | + private List<String> fetchPendingMPAReviews(GoogleAdsClient client, long customerId) { |
| 162 | + List<String> pendingReviews = new ArrayList<>(); |
| 163 | + |
| 164 | + // Create a query that will retrieve all the pending MPA reviews. |
| 165 | + String query = |
| 166 | + "SELECT " |
| 167 | + + "multi_party_auth_review.resource_name, " |
| 168 | + + "multi_party_auth_review.review_id, " |
| 169 | + + "multi_party_auth_review.creation_date_time, " |
| 170 | + + "multi_party_auth_review.request_user_email, " |
| 171 | + + "multi_party_auth_review.operation_type, " |
| 172 | + + "multi_party_auth_review.justification, " |
| 173 | + + "multi_party_auth_review.target_resource, " |
| 174 | + + "multi_party_auth_review.customer_user_access_review.old_customer_user_access, " |
| 175 | + + "multi_party_auth_review.customer_user_access_review.new_customer_user_access, " |
| 176 | + + "multi_party_auth_review.customer_user_access_invitation_review.new_customer_user_access_invitation " |
| 177 | + + "FROM multi_party_auth_review " |
| 178 | + + "WHERE multi_party_auth_review.review_status = 'PENDING'"; |
| 179 | + |
| 180 | + try (GoogleAdsServiceClient googleAdsServiceClient = |
| 181 | + client.getLatestVersion().createGoogleAdsServiceClient()) { |
| 182 | + SearchPagedResponse response = |
| 183 | + googleAdsServiceClient.search(String.valueOf(customerId), query); |
| 184 | + |
| 185 | + for (GoogleAdsRow googleAdsRow : response.iterateAll()) { |
| 186 | + MultiPartyAuthReview mpaReview = googleAdsRow.getMultiPartyAuthReview(); |
| 187 | + System.out.printf( |
| 188 | + "%s created a pending multi-party auth review with ID %d " |
| 189 | + + "at %s. This request is for target resource type = %s and operation type = " |
| 190 | + + "%s. The justification is \"%s\".%n", |
| 191 | + mpaReview.getRequestUserEmail(), |
| 192 | + mpaReview.getMultiPartyAuthReviewId(), |
| 193 | + mpaReview.getCreationDateTime(), |
| 194 | + mpaReview.getTargetResource(), |
| 195 | + mpaReview.getOperationType(), |
| 196 | + mpaReview.getJustification()); |
| 197 | + |
| 198 | + if (mpaReview.getTargetResource() == MultiPartyAuthReviewTargetResource.CUSTOMER_USER_ACCESS) { |
| 199 | + CustomerUserAccessReview accessReview = mpaReview.getCustomerUserAccessReview(); |
| 200 | + if (mpaReview.getOperationType() == MultiPartyAuthOperationType.UPDATE) { |
| 201 | + // When updating a customer user access, only the new access level is populated. |
| 202 | + System.out.printf( |
| 203 | + "Old resource name: %s, new access role: %s.%n", |
| 204 | + accessReview.getOldCustomerUserAccess(), |
| 205 | + accessReview.getNewCustomerUserAccess().getAccessRole()); |
| 206 | + } else if (mpaReview.getOperationType() == MultiPartyAuthOperationType.REMOVE) { |
| 207 | + System.out.printf( |
| 208 | + "Old resource name: %s.%n", accessReview.getOldCustomerUserAccess()); |
| 209 | + } |
| 210 | + } else if (mpaReview.getTargetResource() |
| 211 | + == MultiPartyAuthReviewTargetResource.CUSTOMER_USER_ACCESS_INVITATION) { |
| 212 | + CustomerUserAccessInvitation newInvite = |
| 213 | + mpaReview.getCustomerUserAccessInvitationReview().getNewCustomerUserAccessInvitation(); |
| 214 | + System.out.printf( |
| 215 | + "Invitation email address: %s, Role: %s.%n", |
| 216 | + newInvite.getEmailAddress(), newInvite.getAccessRole()); |
| 217 | + } |
| 218 | + |
| 219 | + pendingReviews.add(mpaReview.getResourceName()); |
| 220 | + } |
| 221 | + } |
| 222 | + return pendingReviews; |
| 223 | + } |
| 224 | + // [END fetch_mpa_review] |
| 225 | +} |
0 commit comments