我正在尝试使用Parse中的Twilio Cloud Module发送短信验证。 我该怎么做。我没有任何关于android的准确教程或使用指南。如何将params发送到云功能?
答案 0 :(得分:3)
您可以在Parse上创建两个Cloud Function,一个用于向用户发送验证码,另一个用于验证用户的电话号码。
要开始使用Parse云函数,请查看本指南https://www.parse.com/docs/js/guide#cloud_modules
设置Parse工具后,转到main.js文件并添加两个函数
// Include the Twilio Cloud Module and initialize it
var twilio = require('twilio')('<Your Twilio Account Sid>', '<Your Twilio Auth Token>');
// Define the Cloud Functions
Parse.Cloud.define("sendVerificationCode", function(request, response) {
var verificationCode = Math.floor(Math.random()*999999);
var user = Parse.User.current();
user.set("phoneVerificationCode", verificationCode);
user.save();
twilio.sendSms({
From: "<Your Twilio phone number>",
To: request.params.phoneNumber,
Body: "Your verification code is " + verificationCode + "."
}, function(err, responseData) {
if (err) {
response.error(err);
} else {
response.success("Success");
}
});
});
Parse.Cloud.define("verifyPhoneNumber", function(request, response) {
var user = Parse.User.current();
var verificationCode = user.get("phoneVerificationCode");
if (verificationCode == request.params.phoneVerificationCode) {
user.set("phoneNumber", request.params.phoneNumber);
user.save();
response.success("Success");
} else {
response.error("Invalid verification code.");
}
});
之后,在您的Android项目中,您可以按照以下方式申请电话验证
HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("sendVerificationCode", params, new FunctionCallback<JSONObject>() {
public void done(JSONObject response, ParseException e) {
progressBar.setVisibility(View.GONE);
if (e == null) {
Log.d("Response", "no exceptions! " + response.toString());
// Code sent successfully you have to wait it or ask the user to enter the code for verification
} else {
Log.d("Response", "Exception: " + response.toString() + e);
Toast.makeText(getApplicationContext(), "Something wrong. Please try again." + e, Toast.LENGTH_LONG).show();
}
}
});
然后验证用户
(接收)/(由用户输入)的代码HashMap<String, Object> params = new HashMap<String, Object>();
params.put("phoneNumber", phoneNumber);
params.put("phoneVerificationCode", code);
progressBar.setVisibility(View.VISIBLE);
ParseCloud.callFunctionInBackground("verifyPhoneNumber", params, new FunctionCallback<String>() {
public void done(String response, ParseException e) {
progressBar.setVisibility(View.GONE);
if (e == null) {
token = response;
Log.d("Response", "no exceptions! " + response);
ParseUser.becomeInBackground(token, new LogInCallback() {
@Override
public void done(ParseUser parseUser, ParseException e) {
if (e == null){
Log.d("Response", "no exceptions! ");
Intent i = new Intent(LoginActivity.this, MainActivity.class);
startActivity(i);
finish();
} else {
Log.d("Response", "Exception: " + e);
Toast.makeText(getApplicationContext(),"Something wrong. Please try again." + e, Toast.LENGTH_LONG).show();
}
}
});
} else {
Log.d("Response", "Exception: " + response + e);
Toast.makeText(getApplicationContext(), "Something wrong. Please try again.", Toast.LENGTH_LONG).show();
}
}
});