Twilio SMs双因素身份验证

时间:2017-04-14 18:18:56

标签: android heroku twilio sms-gateway two-factor-authentication

我目前正尝试在项目中实施双因素身份验证系统,我正在使用twilio作为短信网关服务来请求随机登录令牌,然后将其作为文本消息发送给用户。我按照“https://www.twilio.com/blog/2016/05/how-to-send-an-sms-from-android.html”中的教程来测试服务。在教程之后,我在Heroku上托管了后端。该应用程序工作正常,并说已发送短信。但是我从来没有收到它。任何帮助都会很棒。

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.content.Context;
import java.io.IOException;
import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class MainActivity extends AppCompatActivity {

 private EditText mTo;
 private EditText mBody;
 private Button mSend;
 private OkHttpClient mClient = new OkHttpClient();
 private Context mContext;

 @Override
 protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    mTo = (EditText) findViewById(R.id.txtNumber);
    mBody = (EditText) findViewById(R.id.txtMessage);
    mSend = (Button) findViewById(R.id.btnSend);
    mSend.setOnClickListener(new View.OnClickListener(){
        @Override
        public void onClick(View v) {
            try {
                post(" https://cryptic-shore-79857.herokuapp.com", new 
Callback(){

                    @Override
                    public void onFailure(Call call, IOException e) {
                        e.printStackTrace();
                    }

                    @Override
                    public void onResponse(Call call, Response response) 
 throws IOException {
                        runOnUiThread(new Runnable() {
                            @Override
                            public void run() {
                                mTo.setText("");
                                mBody.setText("");
                                Toast.makeText(getApplicationContext(),"SMS Sent!",Toast.LENGTH_SHORT).show();
                            }
                        });
                    }
                });
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    });
    mContext = getApplicationContext();
}
Call post(String url, Callback callback) throws IOException {
    RequestBody formBody = new FormBody.Builder()
            .add("To", mTo.getText().toString())
            .add("Body", mBody.getText().toString())
            .build();
    Request request = new Request.Builder()
            .url(url)
            .post(formBody)
            .build();
    Call response = mClient.newCall(request);
    response.enqueue(callback);
    return response;
}
}

我认为连接到Heroku的URL不正确,但我不知道应该是什么。

1 个答案:

答案 0 :(得分:0)

Twilio开发者传道者在这里。

您{1}将您的请求POST发送到了错误的网址。目前您的代码执行:

try {
    post("https://cryptic-shore-79857.herokuapp.com", new 
Callback(){

但是发送短信的动作的路径应该是:

try {
    post("https://cryptic-shore-79857.herokuapp.com/sms", new 
Callback(){

注意/sms路径。

让我知道这是否有帮助。