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

Register now to learn Fabric in free live sessions led by the best Microsoft experts. From Apr 16 to May 9, in English and Spanish.

Reply
felixgilioli
New Member

401 Unauthorized when trying to get groups in the Power BI APIs

I need to create an application that will allow you to register multiple users and link them to your Power BI account. In this application I will attach a .pbix file and I will replicate for all users.

 

I created an app on azure to be able to access the Power BI development APIs.

 

To do a test, I'm trying to call the groups API, when I run it directly in the documentation it works. Copying the token (Authorization) that it displays in the documentation and defining it in the request header in my app works too. However, when I try to generate a token it doesn't work.

 

Can anyone tell me what I'm doing wrong? What if what I want to do is possible?

 

 

@RestController
@RequestMapping("/group")
public class GroupsController {

    @GetMapping
    public String getGroups() throws Exception {
        var token = GetAccessToken();

        var rest = new RestTemplate();

        HttpHeaders headers = new HttpHeaders();
        var authorization = "Bearer " + token;
        headers.set("Authorization", authorization);

        var result = rest.exchange("https://api.powerbi.com/v1.0/myorg/groups", HttpMethod.GET, new HttpEntity(headers), String.class);
        return result.getBody();
    }

    private String GetAccessToken() throws Exception {
        AuthenticationContext authenticationContext = new AuthenticationContext(
                "https://login.windows.net/common/oauth2/authorize",
                false,
                Executors.newSingleThreadExecutor()
        );

        var authenticationResult = authenticationContext.acquireToken(
                "https://analysis.windows.net/powerbi/api",
                new ClientCredential(CLIENT_ID, CLIENT_SECRET),
                null
        ).get();
        return authenticationResult.getAccessToken();
    }
}

 

0 REPLIES 0

Helpful resources

Announcements
Microsoft Fabric Learn Together

Microsoft Fabric Learn Together

Covering the world! 9:00-10:30 AM Sydney, 4:00-5:30 PM CET (Paris/Berlin), 7:00-8:30 PM Mexico City

PBI_APRIL_CAROUSEL1

Power BI Monthly Update - April 2024

Check out the April 2024 Power BI update to learn about new features.

April Fabric Community Update

Fabric Community Update - April 2024

Find out what's new and trending in the Fabric Community.