You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

嵌入Power BI到Salesforce:求OAuthController与PowerBIController示例

Power BI嵌入Salesforce的控制器示例指引

OAuthController 示例(Apex)

该控制器用于处理Power BI的OAuth客户端凭证流程,获取服务端访问令牌:

public with sharing class OAuthController {
    // 替换为你的Power BI应用客户端ID
    private static final String CLIENT_ID = 'your-powerbi-client-id';
    // 替换为你的Power BI应用客户端密钥
    private static final String CLIENT_SECRET = 'your-powerbi-client-secret';
    private static final String AUTH_ENDPOINT = 'https://login.microsoftonline.com/common/oauth2/v2.0/token';
    private static final String SCOPE = 'https://analysis.windows.net/powerbi/api/.default';

    @AuraEnabled
    public static String getPowerBIAccessToken() {
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint(AUTH_ENDPOINT);
        req.setMethod('POST');
        req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
        
        String body = 'grant_type=client_credentials' +
                      '&client_id=' + EncodingUtil.urlEncode(CLIENT_ID, 'UTF-8') +
                      '&client_secret=' + EncodingUtil.urlEncode(CLIENT_SECRET, 'UTF-8') +
                      '&scope=' + EncodingUtil.urlEncode(SCOPE, 'UTF-8');
        
        req.setBody(body);
        
        try {
            HttpResponse res = http.send(req);
            if (res.getStatusCode() == 200) {
                Map<String, Object> responseMap = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
                return (String)responseMap.get('access_token');
            } else {
                throw new AuraHandledException('获取Power BI令牌失败: ' + res.getBody());
            }
        } catch (Exception e) {
            throw new AuraHandledException('请求出错: ' + e.getMessage());
        }
    }
}

PowerBIController 示例(Apex)

该控制器用于获取Power BI报表的嵌入配置,供前端渲染使用:

public with sharing class PowerBIController {
    @AuraEnabled
    public static Map<String, Object> getReportEmbedConfig(String reportId, String workspaceId) {
        String accessToken = OAuthController.getPowerBIAccessToken();
        
        Http http = new Http();
        HttpRequest req = new HttpRequest();
        req.setEndpoint('https://api.powerbi.com/v1.0/myorg/groups/' + workspaceId + '/reports/' + reportId + '/GenerateToken');
        req.setMethod('POST');
        req.setHeader('Authorization', 'Bearer ' + accessToken);
        req.setHeader('Content-Type', 'application/json');
        
        // 配置嵌入令牌权限,可根据需求改为Edit等
        String body = '{"accessLevel": "View"}';
        req.setBody(body);
        
        try {
            HttpResponse res = http.send(req);
            if (res.getStatusCode() == 200) {
                Map<String, Object> tokenResponse = (Map<String, Object>)JSON.deserializeUntyped(res.getBody());
                Map<String, Object> embedConfig = new Map<String, Object>();
                embedConfig.put('embedUrl', 'https://app.powerbi.com/reportEmbed?reportId=' + reportId + '&groupId=' + workspaceId);
                embedConfig.put('accessToken', tokenResponse.get('token'));
                embedConfig.put('tokenType', 0); // 0代表嵌入令牌类型
                return embedConfig;
            } else {
                throw new AuraHandledException('获取嵌入配置失败: ' + res.getBody());
            }
        } catch (Exception e) {
            throw new AuraHandledException('请求出错: ' + e.getMessage());
        }
    }
}

关键注意事项

  • 替换代码中的CLIENT_ID、CLIENT_SECRET为你在Azure AD注册的Power BI应用的真实信息。
  • reportId和workspaceId可从Power BI服务的报表URL中提取。
  • 在Salesforce远程站点设置中添加以下域名:login.microsoftonline.com、api.powerbi.com、app.powerbi.com。
  • 前端可通过Lightning组件调用上述控制器方法,拿到嵌入配置后使用Power BI JavaScript SDK渲染报表。

内容的提问来源于stack exchange,提问作者Tristan

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.15 21:20:37