Sample Code from Website

In the sample code below....

UserMe me = usersApi.getMe(Arrays.asList("presence"));

This keeps returning a Null Pointer Exception because usersApi.getMe(....) is returning null.

In my code I am using api.mypurecloud.com instead of api.mypurecloud.ie

Configuration.getDefaultApiClient().setBasePath("https://api.mypurecloud.com");
Configuration.getDefaultApiClient().setAccessToken("using My Access Token here");

Any suggestions on how I can connect to my org and return this info?

Example from the Website:::

// Set access token
Configuration.getDefaultApiClient().setAccessToken("BL4Cb3EQIQFlqIItaj-zf5eyhAiP964kucEuOI1g54dKQIgd24P99ojbFHtpgUTudRIkuUYfXMy0afEnZc5nEQ");

// Set environment
Configuration.getDefaultApiClient().setBasePath("https://api.mypurecloud.ie");

// Instantiate API
UsersApi usersApi = new UsersApi();

// Get the logged in user
UserMe me = usersApi.getMe(Arrays.asList("presence"));
System.out.println("Hello " + me.getName());

Which OAuth flow did you use to get that access token?

I used Postman to get the Token and I pasted that into the code.

But which OAuth type are you using? Implicit Grant, Authorization Code Grant, or Client Credentials Grant?

The Grant Type is set at Code Authorization.

The only time I've seen that is when you're using a token from a client credentials grant; there is no person context for client credentials and therefore /users/me returns nothing. This should be working with code authorization.

Can you try grabbing the latest JAR file from github and try with that? The JAR file is located at https://github.com/MyPureCloud/purecloud_api_sdk_java/tree/master/build/target (as of a few minutes ago). It should be on maven central this week.

For reference, here's my full application I tested with:

package com.inin;

import com.mypurecloud.sdk.ApiException;
import com.mypurecloud.sdk.Configuration;
import com.mypurecloud.sdk.api.UsersApi;
import com.mypurecloud.sdk.model.User;
import com.mypurecloud.sdk.model.UserMe;
import com.mypurecloud.sdk.model.UsersEntityListing;

import java.util.Arrays;

public class Main {

    public static void main(String[] args) {
        try {
            Configuration.getDefaultApiClient().setAccessToken("my token");
            Configuration.getDefaultApiClient().setBasePath("https://api.mypurecloud.com");

            UsersApi usersApi = new UsersApi();

            UserMe me = usersApi.getMe(Arrays.asList("presence"));
            System.out.println("Hello " + me.getName());

            UsersEntityListing users = usersApi.getUsers(25, 1, null, null, Arrays.asList("presence"));
            System.out.println("\nKnown users:");
            for (User user : users.getEntities()) {
                System.out.println(user.getName());
            }
        } catch (ApiException e) {
            e.printStackTrace();
        }
    }
}

I did add the new version. Blew out the existing files and re-imported and it now works.

Thanks

1 Like