Skip to main content
cancel
Showing results for 
Search instead for 
Did you mean: 

The Power BI Data Visualization World Championships is back! Get ahead of the game and start preparing now! Learn more

Reply
iperezal
Frequent Visitor

Rest api usage with Java

There is any code example about how to use Rest api from Java?

1 ACCEPTED SOLUTION

@iperezal

 

Check this Github repository satalyst/powerbi-rest-java.

The code snippet

 

public class Office365Authenticator implements Authenticator {
    private static final String DEFAULT_AUTHORITY = "https://login.windows.net/common/oauth2/authorize";
    private static final String DEFAULT_POWER_BI_RESOURCE_ID = "https://analysis.windows.net/powerbi/api";
    private static final boolean DEFAULT_VALIDATE_AUTHORITY = false;
    private String authority = DEFAULT_AUTHORITY;
    private String powerBiResourceId = DEFAULT_POWER_BI_RESOURCE_ID;
    private boolean validateAuthority = DEFAULT_VALIDATE_AUTHORITY;
    private String nativeClientId;
    private String tenant;
    private String username;
    private String password;

    private ExecutorService executor;
    private ReadWriteLock tokenLock = new ReentrantReadWriteLock();
    private String cachedToken;

    private String _authenticate() throws AuthenticationFailureException {
        try {
            AuthenticationContext authenticationContext = new AuthenticationContext(
                    authority,
                    validateAuthority,
                    executor
            );

            String result = getAccessToken(
                    authenticationContext,
                    powerBiResourceId,
                    nativeClientId,
                    username + "@" + tenant,
                    password
            );

            if (StringUtils.isEmpty(result)) {
                throw new AuthenticationFailureException("Returned access token is null.");
            }

            return result;
        } catch (ExecutionException | InterruptedException | IOException e) {
            throw new AuthenticationFailureException(e);
        }
    }

    private String getAccessToken(AuthenticationContext authenticationContext, String resourceId, String clientId,
                                  String username, String password) throws ExecutionException, InterruptedException {
        return authenticationContext.acquireToken(
                resourceId,
                clientId,
                username,
                password,
                null
        ).get().getAccessToken();
    }
}

 

 

View solution in original post

7 REPLIES 7
gopinath
New Member

Hi....

 

Any body give the java code for create dataset using power bi API...Advance in thanks...

 

Regards,

Gopinath M

Eric_Zhang
Microsoft Employee
Microsoft Employee

@iperezal

 

Various resource on the Internet.

RESTful Java client with java.net.URL

Using REST in Java

Consuming REST services from Java applications

 

You could find more examples in Github.

But, there aren't any examples about PowerBI Rest API and his Auth in Java 😞

 

I cannot authenticate from Java. I only can from C# and i need Java code

@iperezal

 

Check this Github repository satalyst/powerbi-rest-java.

The code snippet

 

public class Office365Authenticator implements Authenticator {
    private static final String DEFAULT_AUTHORITY = "https://login.windows.net/common/oauth2/authorize";
    private static final String DEFAULT_POWER_BI_RESOURCE_ID = "https://analysis.windows.net/powerbi/api";
    private static final boolean DEFAULT_VALIDATE_AUTHORITY = false;
    private String authority = DEFAULT_AUTHORITY;
    private String powerBiResourceId = DEFAULT_POWER_BI_RESOURCE_ID;
    private boolean validateAuthority = DEFAULT_VALIDATE_AUTHORITY;
    private String nativeClientId;
    private String tenant;
    private String username;
    private String password;

    private ExecutorService executor;
    private ReadWriteLock tokenLock = new ReentrantReadWriteLock();
    private String cachedToken;

    private String _authenticate() throws AuthenticationFailureException {
        try {
            AuthenticationContext authenticationContext = new AuthenticationContext(
                    authority,
                    validateAuthority,
                    executor
            );

            String result = getAccessToken(
                    authenticationContext,
                    powerBiResourceId,
                    nativeClientId,
                    username + "@" + tenant,
                    password
            );

            if (StringUtils.isEmpty(result)) {
                throw new AuthenticationFailureException("Returned access token is null.");
            }

            return result;
        } catch (ExecutionException | InterruptedException | IOException e) {
            throw new AuthenticationFailureException(e);
        }
    }

    private String getAccessToken(AuthenticationContext authenticationContext, String resourceId, String clientId,
                                  String username, String password) throws ExecutionException, InterruptedException {
        return authenticationContext.acquireToken(
                resourceId,
                clientId,
                username,
                password,
                null
        ).get().getAccessToken();
    }
}

 

 

@Eric_Zhang
In this code snippet   

authority, validateAuthority, executor (authenticationContext )? - How should i fill these values? what do they means ?

powerBiResourceId? - Where is this id , what does it means?
nativeClientId - I'm making a web app (not native app) and this will be ApplicationId in  Azure? 
username + "@" + tenant - I know this
password - I know this

Hello,

 

I just created the API using above java code and I dont how to check the above mentioned maven project...

can you please provided step by step instructions to check the above API?I.e create dataset,add dataset..

 

Thanks,

Gopinath Madhavan

 

Azure AD has a Java library that you can use: https://github.com/AzureAD/azure-activedirectory-library-for-java

Helpful resources

Announcements
November Power BI Update Carousel

Power BI Monthly Update - November 2025

Check out the November 2025 Power BI update to learn about new features.

Fabric Data Days Carousel

Fabric Data Days

Advance your Data & AI career with 50 days of live learning, contests, hands-on challenges, study groups & certifications and more!

FabCon Atlanta 2026 carousel

FabCon Atlanta 2026

Join us at FabCon Atlanta, March 16-20, for the ultimate Fabric, Power BI, AI and SQL community-led event. Save $200 with code FABCOMM.