命名凭证

Named Credential

我想访问指定凭据的 URL 并想动态地向其添加一些查询参数。可能吗?与自定义设置的情况一样,我通过直接获取值来访问该值。 named credential global variable credential 是否为我提供了这样的机会?

请参阅此处的文档:

https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_callouts_named_credentials.htm

本例使用:

HttpRequest req = new HttpRequest();
req.setEndpoint('callout:***My_Named_Credential***/some_path');

只要您的命名凭证 URL 包含协议、子域(如果需要)、主机名和端口(需要 id),您就可以附加路径、参数或片段。

例如:

Map<String, String> urlParameters = new Map<String, String>{'client_id','12345'};
String urlParameterString = '';
for (String param : urlParameters.keySet()) {
    urlParameterString += param+'='+urlParameters.get(param);
}

HttpRequest req = new HttpRequest();
req.setEndpoint(
    String.format(
        'callout:GoogleAPI/{0}?{1}#{2}',
        new String[]{'oauth2', urlParameterString, 'anchorId'}
    )
);

如果命名凭证 URL 是:

https://api.google.com

那个端点应该是这样的:

https://api.google.com/oauth2?client_id=12345#anchorId