java - 执行失败 Twilio SMS (Java)
问题描述
我正在尝试使用 Twilio 发送 SMS 消息。我已按照所有说明操作并设置了 JAVA_HOME 环境变量。当我运行程序时,我得到:
任务“:StockAlertClass:SMS.main()”执行失败。
进程 'command'/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/java'' 以非零退出值 1 结束
尝试:使用 --info 或 --debug 选项运行以获得更多日志输出。运行 --scan 以获得完整的见解。
例外是:org.gradle.api.tasks.TaskExecutionException:任务':StockAlertClass:SMS.main()'的执行失败。<36 内部调用> 引起:org.gradle.process.internal.ExecException: Process 'command '/Library/Java/JavaVirtualMachines/jdk1.8.0_212.jdk/Contents/Home/bin/java'' 以非零完成退出值 1<98 内部调用>
我的java类是:
import com.twilio.Twilio;
import com.twilio.rest.api.v2010.account.Message;
import com.twilio.type.PhoneNumber;
public class SMS {
// Find your Account SID and Auth Token at twilio.com/console
// and set the environment variables. See http://twil.io/secure
public static final String ACCOUNT_SID = System.getenv("HiddenForPrivacy");
public static final String AUTH_TOKEN = System.getenv("HiddenForPrivacy");
public static void Message() {
Twilio.init(ACCOUNT_SID, AUTH_TOKEN);
Message message = Message.creator(new PhoneNumber("+HiddenForPrivacy"),
new PhoneNumber("HiddenForPrivacy"),
"Test ").create();
System.out.println(message.getSid());
}
public static void main(String[] args) {
Message();
}
}
build.gradle 是:
plugins {
id 'java'
}
group 'org.example'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
mavenLocal()
}
if (hasProperty('buildScan')) {
buildScan {
termsOfServiceUrl = 'https://gradle.com/terms-of-service'
termsOfServiceAgree = 'yes'
}
}
dependencies {
compile 'org.slf4j:slf4j-simple:1.6.+'
compile 'com.sparkjava:spark-core:2.5.+'
implementation group: "com.twilio.sdk", name: "twilio", version: "8.21.0"
testImplementation 'org.junit.jupiter:junit-jupiter-api:5.6.0'
testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine'
// https://mvnrepository.com/artifact/com.twilio.sdk/twilio
runtimeOnly group: 'com.twilio.sdk', name: 'twilio', version: '8.21.0'
}
test {
useJUnitPlatform()
}
解决方案
Twilio 开发人员布道者在这里。
我很关心你是如何分享这些台词的:
public static final String ACCOUNT_SID = System.getenv("HiddenForPrivacy");
public static final String AUTH_TOKEN = System.getenv("HiddenForPrivacy");
您不必在这些行中隐藏密钥。相反,您应该将 Account SID 和 Auth Token 设置为调用的环境变量ACCOUNT_SID
,AUTH_TOKEN
然后您的代码应如下所示:
public static final String ACCOUNT_SID = System.getenv("ACCOUNT_SID");
public static final String AUTH_TOKEN = System.getenv("AUTH_TOKEN");
如果您在这些调用中使用了实际的 Account Sid 和 Auth Token,那么我的猜测是您的代码失败,因为它们设置不正确。
我建议阅读这篇关于在 Java 中使用环境变量的博客文章以获取更多详细信息。
推荐阅读
- html - 为绝对定位元素设置高度 =“100% - 相对顶部”
- intellij-idea - 两个打开的项目占用内存堆的两倍还是分开了?
- docker - 如何在 Docker 中的 wireguard VPN 服务上托管 nextcloud 容器?
- python - f_classif (sklearn) 的输出是一个仅包含 nan 的数组
- flutter - Flutter 桌面,拥有电脑独有的信息
- android - 如何在recyclerview中加载数据?
- r - RStudio:迁移到新 PC 时接管已安装的软件包和基本设置
- javascript - 从先前单击的链接中删除边框/样式 - React/Nextjs
- java - java中的数据类型和计算平均值
- java - Springboot logback不删除旧日志文件