Code:

GoogleCredentials credentials = GoogleCredentials.fromStream
(new 
FileInputStream("robotic-aviary.json")).createScoped("https://www.googleapis.com/auth/adwords";,
 "https://www.googleapis.com/auth/adsensehost";, 
"https://www.googleapis.com/auth/adsense";);
 
credentials.refreshIfExpired();
GoogleAdsClient googleAdsClient = GoogleAdsClient.newBuilder
().setCredentials(credentials).setDeveloperToken(developerToken
).setLoginCustomerId(Long.valueOf(customerId)).build();
try (GoogleAdsServiceClient googleAdsServiceClient =
googleAdsClient.getLatestVersion().createGoogleAdsServiceClient()) {
String query = "SELECT campaign.id, campaign.name FROM campaign ORDER BY 
campaign.id";
// Constructs the SearchGoogleAdsStreamRequest.
SearchGoogleAdsStreamRequest request =
SearchGoogleAdsStreamRequest.newBuilder()
.setCustomerId(customerId)
.setQuery(query)
.build();

// Creates and issues a search Google Ads stream request that will retrieve 
all campaigns.
ServerStream<SearchGoogleAdsStreamResponse> stream =
googleAdsServiceClient.searchStreamCallable().call(request);

// Iterates through and prints all of the results in the stream response.
for (SearchGoogleAdsStreamResponse response : stream) {
for (GoogleAdsRow googleAdsRow : response.getResultsList()) {
System.out.printf(
"Campaign with ID %d and name '%s' was found.%n",
googleAdsRow.getCampaign().getId(), googleAdsRow.getCampaign().getName());
}
}

Log:
Request
-------
MethodName: google.ads.googleads.v10.services.GoogleAdsService/SearchStream
Endpoint: googleads.googleapis.com:443
Headers: {developer-token=REDACTED, login-customer-id=134xxxxxx, 
x-goog-api-client=gl-java/1.8.0_60 gccl/17.0.1 gapic/17.0.1 gax/2.6.1 
grpc/1.27.2}
Body: customer_id: " 134xxxxxx  "
query: "SELECT campaign.id, campaign.name FROM campaign ORDER BY 
campaign.id"


Response
--------
Headers: 
Metadata(content-type=application/grpc,request-id=6vd9tHbU6pTfNbX_KtJ6PQ,date=Sat,
 
02 Apr 2022 06:25:39 GMT,alt-svc=h3=":443"; ma=2592000,h3-29=":443"; 
ma=2592000,h3-Q050=":443"; ma=2592000,h3-Q046=":443"; 
ma=2592000,h3-Q043=":443"; ma=2592000,quic=":443"; ma=2592000; v="46,43")
Body: null
Failure message: errors {
  error_code {
    authentication_error: NOT_ADS_USER
  }
  message: "User in the cookie is not a valid Ads user."
}
request_id: "6vd9tHbU6pTfNbX_KtJ6PQ"


在2022年4月2日星期六 UTC+8 14:33:59<XiuNan Liu> 写道:

> When calling ads API with service account, I returned error getting access 
> token for service account: 401 unauthorized
>
>

-- 
-- 
=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~=~
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 
"Google Ads API and AdWords API Forum" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/adwords-api/9ea04d06-899e-4e84-860c-ec91d11a52f1n%40googlegroups.com.
  • au... 刘修南
    • ... XiuNan Liu
      • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum
        • ... XiuNan Liu
          • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum
            • ... XiuNan Liu
            • ... XiuNan Liu
              • ... XiuNan Liu
                • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum
                • ... XiuNan Liu
                • ... 'Google Ads API Forum Advisor' via Google Ads API and AdWords API Forum

Reply via email to