401 ApiException

I'm trying to get FlowAggregation data using postAnalyticsFlowsAggregatesQuery() method,
but got an ApiException: error as below .

【Error Log】
Authentication successful. Access token expires in 86399 seconds
HANDLE YOUR AUTH TOKENS LIKE PASSWORDS
401
null
{X-Cache=Error from cloudfront, Strict-Transport-Security=max-age=600; includeSubDomains, Cache-Control=no-cache, no-store, must-revalidate, X-Amz-Cf-Pop=KIX50-P3, Connection=keep-alive, X-Amz-Cf-Id=c1wjgS8U8M8zVmKbK464YJTPsUpJHrwES_JB45DVtdLKA7H3Tmnb1w==, Content-Length=154, ININ-Correlation-Id=7c2e717f-9461-4e27-bd3f-ad2d50b33479, Date=Wed, 12 Oct 2022 00:53:27 GMT, Content-Type=application/json, Via=1.1 cb9f42f368829a4cc310e5102b139936.cloudfront.net (CloudFront)}
com.mypurecloud.sdk.v2.ApiException: error
at com.mypurecloud.sdk.v2.ApiClient.interpretConnectorResponse(ApiClient.java:704)
at com.mypurecloud.sdk.v2.ApiClient.getAPIResponse(ApiClient.java:756)
at com.mypurecloud.sdk.v2.ApiClient.invoke(ApiClient.java:867)
at com.mypurecloud.sdk.v2.api.FlowsApi.postAnalyticsFlowsAggregatesQuery(FlowsApi.java:80)
at com.mypurecloud.sdk.v2.api.FlowsApi.postAnalyticsFlowsAggregatesQuery(FlowsApi.java:49)
at csc.batch.main.reportdata.report.GetTokenAndGetFlowInfo.main(GetTokenAndGetFlowInfo.java:76)

【Application detail】
1.Using an clientId and clientSecret,I got a token code sucessfully through authorizeClientCredentials() method as below.
→ApiResponse authResponse = apiClient1.authorizeClientCredentials(clientId, clientSecret);
String token = authResponse.getBody().getAccess_token();→i can got an token code successfully and it expires in 86399 seconds.

2.Using the token code got from above ,I build a new apiClinet as below.
→ ApiClient apiClient =Builder.standard()
.withAccessToken(token)
.withBasePath("https://api.mypurecloud.com")
.build();
Configuration.setDefaultApiClient(apiClient);

3.Using the method below I tried to get FlowAggregation data from genesysCloud.
→ FlowsApi apiInstance = new FlowsApi();
FlowAggregationQuery flowBody= new FlowAggregationQuery();

		flowBody.setInterval("2022-05-06T00:00:00.000Z/2022-05-06T12:00:00.000Z");
		flowBody.setGranularity("");
		flowBody.setTimeZone("");
		flowBody.setGroupBy(null);
		flowBody.setFilter(null);
		flowBody.setMetrics(null);
		flowBody.setFlattenMultivaluedDimensions(true);
		flowBody.setViews(null);
		flowBody.setAlternateTimeDimension(null);

		FlowAggregateQueryResponse result = apiInstance.postAnalyticsFlowsAggregatesQuery(flowBody);
		→I got a 401 StatusCode here.

【more information】
・java version "1.8.0_341"
・platform-client-v2-155.0.0.jar
・the website my appication based on:
→about acess token
https://developer.genesys.cloud/devapps/sdk/java
→about analytics data
https://developer.genesys.cloud/analyticsdatamanagement/analytics/analytics-apis

I've checked many similar questions upload on the Genesys Cloud Developer Forum,
but Still can't have my problem solved.

I'd apreciate it if you can help me find what I am doing wrong. Thank you!

A 401 response means either you're not sending an auth token with the request or the one you're sending isn't valid. Check your code to make sure all of your variables are scoped correctly and you're making requests to the correct environment.

Please be aware that you have posted your auth token in a public forum potentially exposing your org to unauthorized access. Auth tokens must always be handled like passwords; they provide access to your org.

1 Like

Thank you for your reply.

Just as what you advised it seems the environment which I requested to was wrong.
So I changed my code as below.But this time I got a 403 status code.
Actually I am using an auth token ,and it will be created everytime I execute my applicaiton.

would you advise me more what may be wrong now,thank you !
【Before】
→ ApiClient apiClient =Builder.standard()
.withAccessToken(token)
.withBasePath("https://api.mypurecloud.com")
.build();

【After】
→ ApiClient apiClient =Builder.standard()
.withAccessToken(token)
.withBasePath("https://api.mypurecloud.jp")
.build();
(Also thank you for editing my token,I will be careful next time.)

403 means you did send a valid auth token, but you're not allowed to access the resource. Check the documentation for the API resource you're using to see what permissions and scopes it requires. If you look at the API response body, it will typically indicate exactly why you were rejected.

1 Like

Hi,thank you for your advices.

I need more help!
Woud you please give me more advices about the two questions under below!

403 means you did send a valid auth token, but you're not allowed to access the resource.
→Question1:Could you give me more information about how to add permisions to access the resource?

Check the documentation for the API resource you're using to see what permissions and scopes it requires.
→Question2:Could you share me the link of the documentation you reffered to above?
My application takes references from the documentation under below.But it doesn't mention any thing about permissions .
https://developer.genesys.cloud/analyticsdatamanagement/analytics/aggregate/

If you look at the API response body, it will typically indicate exactly why you were rejected.
→Everytime my application returns me back a null response body.(cry)

Thank you ,sincerely!

https://developer.genesys.cloud/devapps/api-explorer

It does:

1 Like

Thank you so much!
This is really very help!

Sorry, still I need help on this case.

We checked the permissions added to the Auth client we use,
and it does have a permission to the resource we are going to access.

Do you know any other solutions which can deal with the 403 error,
thank you !

Can you share the full response body from the failed request please?

1.The error status code changed from 403 to 400 today.
When I execute the same application as last time today,
it returned me a 400 code.
( Maybe the Admin user changed the role or permisions, but I'm not sure.)

2.I added some method to get more information about the ApiException .
■the changes I made to my appication
→before
}catch (ApiException e) {
System.out.println(e.getStatusCode());
System.out.println(e.getBody());
System.out.println(e.getHeaders());
e.printStackTrace();
}
→after
}catch (ApiException e) {
System.out.println("e.getStatusCode():"+e.getStatusCode());
System.out.println("e.getBody():"+e.getBody());
System.out.println("e.getHeaders():"+e.getHeaders());
System.out.println("e.getException():"+e.getException());
System.out.println("e.getStatusReasonPhrase();:"+e.getStatusReasonPhrase());
System.out.println("e.getLocalizedMessage():"+e.getLocalizedMessage());
System.out.println("e.getMessage():"+e.getMessage());
System.out.println("e.getException():"+e.getException());
System.out.println("e.getCause():"+e.getCause());
System.out.println("e.getCorrelationId():"+e.getCorrelationId());
System.out.println("e.getRawBody():"+e.getRawBody());

		e.printStackTrace();			
	}	

3.The detail of the error now.
e.getCorrelationId():0b619df2-7c4b-4db0-acb0-9bb9eb3871c2
e.getRawBody():{"message":"interval is required","code":"bad.request","status":400,"messageParams":{},"contextId":"0b619df2-7c4b-4db0-acb0-9bb9eb3871c2","details":[],"errors":[]}
com.mypurecloud.sdk.v2.ApiException: error
at com.mypurecloud.sdk.v2.ApiClient.interpretConnectorResponse(ApiClient.java:704)
at com.mypurecloud.sdk.v2.ApiClient.getAPIResponse(ApiClient.java:756)
at com.mypurecloud.sdk.v2.ApiClient.invoke(ApiClient.java:867)
at com.mypurecloud.sdk.v2.api.FlowsApi.postAnalyticsFlowsAggregatesQuery(FlowsApi.java:80)
at com.mypurecloud.sdk.v2.api.FlowsApi.postAnalyticsFlowsAggregatesQuery(FlowsApi.java:49)
at csc.batch.main.reportdata.report.sample.GetTokenAndGetFlowInfo_1.main(GetTokenAndGetFlowInfo_1.java:69)

Do you know which docmentation I shoud take a reference to when I set parameters to the requestBody,thank you !

A 400 response means you sent an invalid request. The response body contains the error message indicating what was wrong with the request. You can find the Java SDK documentation here: https://developer.genesys.cloud/devapps/sdk/docexplorer/purecloudjava/

You can try out API requests in your browser, which may be easier than debugging your app, using API Explorer: https://developer.genesys.cloud/devapps/api-explorer.

1 Like

Thank you for your advice!

I rechecked my coding, and found some of my parameter wasn't correctly setted.
After I corrected my parameters'setting like below, I got the response data successfully this time.

■Request Body Setting(Parameter setting)
FlowsApi apiInstance = new FlowsApi();
FlowAggregationQuery flowBody= new FlowAggregationQuery();
flowBody.setInterval("2022-10-06T00:00:00.000Z/2022-10-07T12:00:00.000Z");
flowBody.setGranularity("PT30M");
List metricsEnum = new ArrayList<>();
metricsEnum.add(MetricsEnum.NFLOW);
metricsEnum.add(MetricsEnum.NFLOWMILESTONE);
metricsEnum.add(MetricsEnum.NFLOWOUTCOME);
metricsEnum.add(MetricsEnum.NFLOWOUTCOMEFAILED);
metricsEnum.add(MetricsEnum.OFLOWMILESTONE);
metricsEnum.add(MetricsEnum.TFLOW);
metricsEnum.add(MetricsEnum.TFLOWDISCONNECT);
metricsEnum.add(MetricsEnum.TFLOWEXIT);
metricsEnum.add(MetricsEnum.TFLOWOUTCOME);
flowBody.setMetrics(metricsEnum);

Thank you again for your help!

This topic was automatically closed 31 days after the last reply. New replies are no longer allowed.