【发布时间】:2018-08-13 07:27:40
【问题描述】:
我想从 Twilio 发送 SMS,注意到他们有为 Java、.Net、Node 等构建的库,因此如果我们掌握这些技术,我们可以使用它们。
但我想从 Salesforce Apex 做同样的事情,并试图弄清楚如何构建 Http 参数以进行授权。
我尝试使用 Twilio 文档中给出的 cURL 示例进行映射,但找不到 Auth 令牌的标头键。
以下是我当前的代码,正在寻找如何设置身份验证参数。
req.setEndpoint(baseURL + '/2010-04-01/Accounts/account_sid/Messages.json');
req.setMethod('POST');
req.setHeader('to', EncodingUtil.urlEncode('+to_number', 'UTF-8'));
req.setHeader('from', EncodingUtil.urlEncode('+from_number', 'UTF-8'));
Http ht = new Http();
HttpResponse res = ht.send(req);
更新请求:
Blob headerValue = Blob.valueOf('my-twilio-account-sid:my-twilio-auth-token');
String authorizationHeader = 'BASIC ' + EncodingUtil.base64Encode(headerValue);
req.setHeader('Authorization', authorizationHeader);
req.setHeader('Content-Type', 'application/x-www-form-urlencoded');
String body = EncodingUtil.urlEncode('From=+from_number&To=+to_number&Body=Sample text from twilio', 'UTF-8');
req.setBody(body);
Http ht = new Http();
HttpResponse res = ht.send(req);
回应说
错误请求:需要“发件人”电话号码。
【问题讨论】:
标签: salesforce twilio apex