如何以静态方法获取会话ID

发布于 2025-01-22 01:05:13 字数 968 浏览 0 评论 0 原文

我正在研究包括LWC和Tooling API的事情。我写了以下方法,该方法发出了标注。但是,当我当时从LWC调用此方法此方法时,我无法获得会话ID,但是如果我从开发人员控制台调用相同的方法,则可以正常工作。

Apex Code:
@AuraEnabled 
public static string getList(String fieldName){  
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    System.debug('res------>'+UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');
    req.setEndpoint('callout:Tooling_Query/query/?q=Select+id,Namespaceprefix,developername,TableEnumOrId+FROM+customfield+Where+developername+LIKE\'' +fieldName+ '\'');
    req.setMethod('GET');
    Http h = new Http();
    HttpResponse res = h.send(req);
    System.debug('res------>'+res.getBody());       
    return res.getBody();
}

当我从LWC调用它时,它将返回此

[{“消息”:“此会话不适合与REST API”,“ errorCode”:“ Invalid_Session_Id”}],

因此,我如何从LWC中获得会话-ID,我已经设置了一个连接的应用程序,并以tooling_query的名称命名凭据 并将URL添加到远程站点。

请在这里帮助我。

I am working on something which includes LWC with tooling API. I wrote this below method which makes a callout. but when I call this method this method from lwc at that time I'm unable to get session Id, but if I call this same method from the developer console then it works fine.

Apex Code:
@AuraEnabled 
public static string getList(String fieldName){  
    HttpRequest req = new HttpRequest();
    req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
    System.debug('res------>'+UserInfo.getSessionID());
    req.setHeader('Content-Type', 'application/json');
    req.setEndpoint('callout:Tooling_Query/query/?q=Select+id,Namespaceprefix,developername,TableEnumOrId+FROM+customfield+Where+developername+LIKE\'' +fieldName+ '\'');
    req.setMethod('GET');
    Http h = new Http();
    HttpResponse res = h.send(req);
    System.debug('res------>'+res.getBody());       
    return res.getBody();
}

When I call it from lwc it returns this

[{"message":"This session is not valid for use with the REST API","errorCode":"INVALID_SESSION_ID"}]

so, how can I get session-id from lwc, I already set up a Connected App and Named Credential by the name of Tooling_Query
and add URL to remote sites.

please help me here.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

揪着可爱 2025-01-29 01:05:13

你不能。您在Lightning Web组件上下文中调用的Apex代码无法获取API启用的会话ID。这是

通过安全策略,闪电组件创建的会话未启用API访问。该限制可防止您的顶点代码对Salesforce进行API调用。使用特定API调用的命名凭据可以仔细,有选择地绕过此安全限制。

对启用API的会议的限制并不是偶然的。仔细查看使用命名凭证以确保您不会创建漏洞的任何代码。

唯一受支持的方法是使用被认证为特定用户。


周围有一个hack浮动,它利用了一个Visualforce页面,从此类顶点上下文中获取会话ID。我不建议这样做,特别是如果您需要访问特权工具API。使用正确的解决方案并构建一个命名的凭据。

You can't. Your Apex code called in a Lightning Web Components context cannot get an API-enabled Session Id. This is documented in the Lightning Web Components Dev Guide:

By security policy, sessions created by Lightning components aren’t enabled for API access. This restriction prevents even your Apex code from making API calls to Salesforce. Using a named credential for specific API calls allows you to carefully and selectively bypass this security restriction.

The restrictions on API-enabled sessions aren’t accidental. Carefully review any code that uses a named credential to ensure you’re not creating a vulnerability.

The only supported approach is to use a Named Credential authenticated as a specific user.


There is a hack floating around that exploits a Visualforce page to obtain a Session Id from such an Apex context. I do not recommend doing this, especially if you need to access the privileged Tooling API. Use the correct solution and build a Named Credential.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文