我遵循了通过Orange API发送短信的教程。我使用Intellij Idea和scala的playframework来做到这一点。
以下是实现实现身份验证并发送短信(OSmsOAuthService.java)的方法的代码:
package external.services;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.sun.org.apache.xerces.internal.impl.dv.util.*;
import static play.libs.F.Promise;
import static play.libs.F.Function;
import play.Logger;
import play.libs.Json;
import play.libs.WS;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
/**
* Created by shadow on 05/10/2018.
*/
public class OSmsOAuthService {
private String clientId;
private String clientSecret;
public OSmsOAuthService (String clientId, String clientSecret) {
this.clientId = clientId;
this.clientSecret = clientSecret;
}
private String bearedEncode() {
String concatCredentials = null;
try{
//Encode CLientID and ClientSecret
String encodedClientId = URLEncoder.encode(clientId,"utf-8");
String encodedClientSecret = URLEncoder.encode(clientSecret,"utf-8");
concatCredentials = encodedClientId+":"+encodedClientSecret;
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
return Base64.encode(concatCredentials.getBytes());
}
public Promise<JsonNode> authenticate () {
WS.WSRequestHolder req = WS.url("https://api.orange.com/oauth/v2/token")
.setHeader("Authorization"," Basic "+bearedEncode())
.setContentType("application/x-www-form-urlencoded;charset=UTF-8");
return req.post("grant_type=client_credentials").map(new Function<WS.Response, JsonNode>() {
@Override
public JsonNode apply(WS.Response response) throws Throwable {
return response.asJson();
}
});
}
public Promise<JsonNode> sendSMSNow(String address, String senderAddress, String message){
String SMS_ENDPOINT = "https://api.orange.com/smsmessaging/v1/outbound/";
return authenticate().flatMap(new Function<JsonNode, Promise<JsonNode>>() {
@Override
public Promise<JsonNode> apply(JsonNode jsonNode) throws Throwable {
String tokenType = jsonNode.findPath("token_type").asText();
String accessToken = jsonNode.findPath("access_token").asText();
if (tokenType.equals("Bearer")) {
try {
String senderEncoded = URLEncoder.encode(senderAddress, "utf-8");
WS.WSRequestHolder req = WS.url(SMS_ENDPOINT+senderAddress+"/requests")
.setHeader("Authorization", "Bearer "+accessToken)
.setHeader("Content-Type", "application/json");
ObjectNode bodyRequest = Json.newObject();
ObjectNode jsonNodesSMS = Json.newObject();
jsonNodesSMS.put("address", URLEncoder.encode(address,"utf-8"));
ObjectNode outboundSMSTextMessage = Json.newObject();
outboundSMSTextMessage.put("message",message);
jsonNodesSMS.put("outboundSMSTextMessage",outboundSMSTextMessage);
jsonNodesSMS.put("senderAddress",senderAddress);
bodyRequest.put("outboundSMSMessageRequest",jsonNodesSMS);
return req.post(bodyRequest).map(new Function<WS.Response, JsonNode>() {
@Override
public JsonNode apply(WS.Response response) throws Throwable {
Logger.info(response.asJson().toString());
return response.asJson();
}
});
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}else {
Logger.error(jsonNode.toString());
return Promise.pure(jsonNode);
}
return Promise.pure(jsonNode);
}
});
}
}
以下是测试身份验证正常工作的代码(Application.java):
package controllers;
import com.fasterxml.jackson.databind.JsonNode;
import external.services.OSmsOAuthService;
import play.*;
import play.libs.F;
import play.libs.F.Promise;
import play.libs.Json;
import play.mvc.*;
import views.html.*;
import java.util.*;
import static play.mvc.Results.*;
public class Application extends Controller {
static OSmsOAuthService oSmsOAuthService;
public static Promise<Result> index() {
String clientId = Play.application().configuration().getString("orangesmsapi.client.id");
String clientSecret = Play.application().configuration().getString("orangesmsapi.client.secret");
String address = Play.application().configuration().getString("orangesmsapi.address");
String senderAddress = Play.application().configuration().getString("orangesmsapi.senderAddress");
String message = Play.application().configuration().getString("orangesmsapi.message");
oSmsOAuthService = new OSmsOAuthService(clientId,clientSecret);
// return oSmsOAuthService.sendSMSNow(address,senderAddress,message).flatMap(new F.Function<JsonNode, Promise<Result>>() {
// @Override
// public Promise<Result> apply(JsonNode jsonNode) throws Throwable {
//
// return Results.ok(jsonNode);
//
//
//}
return oSmsOAuthService.authenticate().flatMap(new F.Function<JsonNode, Promise<Result>>() {
@Override
public Promise<Result> apply(JsonNode jsonNode) throws Throwable {
return Promise.promise(new F.Function0<Result>() {
@Override
public Result apply() throws Throwable {
return Results.ok(jsonNode);
}
});
}
});
}
}
您能帮助您知道如何在Application.java文件上测试sendSMSNow方法。而且,这对于发送一条短信,如何执行多次发送。