跳到主要内容

参考代码 - Java/Android

Maven依赖:

<dependencies>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-api</artifactId>
<version>0.11.5</version>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-impl</artifactId>
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>io.jsonwebtoken</groupId>
<artifactId>jjwt-jackson</artifactId> <!-- or jjwt-gson if Gson is preferred -->
<version>0.11.5</version>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.10.0</version>
</dependency>
</dependencies>

示例代码:

import javax.crypto.SecretKey;
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.*;

import io.jsonwebtoken.*;
import io.jsonwebtoken.security.Keys;

import java.util.Date;
import java.util.Map;

import okhttp3.*;

import java.io.IOException;


public class EzrevenueClient {
private final String projectId;
private final String projectSecret;

private static final String BASE_URL = "https://revenue.ezboti.com/api/v1/server";

public EzrevenueClient(String projectId, String projectSecret) {
this.projectId = projectId;
this.projectSecret = projectSecret;
}

private SecretKey getJwtKey() {
return Keys.hmacShaKeyFor(this.projectSecret.getBytes(StandardCharsets.UTF_8));
}

private SignatureAlgorithm getAlgorithm() {
return SignatureAlgorithm.HS256;
}

private String generateNonce() {
return UUID.randomUUID().toString().replace("-", "");
}

public String encodeToken(Map<String, Object> payload) {
Map<String, Object> claims = new HashMap<>(payload);
claims.put("nonce", this.generateNonce());
Instant exp = Instant.now().plusSeconds(30 * 60);
String token = Jwts.builder()
.setHeaderParam(Header.TYPE, Header.JWT_TYPE)
.setHeaderParam("project_id", this.projectId)
.setClaims(claims)
.setExpiration(Date.from(exp))
.signWith(this.getJwtKey(), this.getAlgorithm())
.compact();
return token;
}

public Map<String, Object> decodeToken(String token) {
Jws<Claims> jwt = Jwts.parserBuilder()
.setSigningKey(this.getJwtKey())
.build()
.parseClaimsJws(token);
Header header = jwt.getHeader();
Claims payload = jwt.getBody();
if (!payload.containsKey("exp")) {
throw new MissingClaimException(header, payload, "claim exp required");
}
if (!payload.containsKey("nonce")) {
throw new MissingClaimException(header, payload, "claim nonce required");
}
return payload;
}

private String sendRequest(String url, String content) throws IOException {
MediaType CONTENT_TYPE = MediaType.parse("text/plain");
OkHttpClient client = new OkHttpClient();
Request request = new Request.Builder()
.url(url)
.post(RequestBody.create(content, CONTENT_TYPE))
.build();
try (Response response = client.newCall(request).execute()) {
ResponseBody body = response.body();
if (!response.isSuccessful() || body == null) {
String detail = "Unexpected code " + response;
if (body != null) {
detail += " Body=" + body.string();
}
throw new IOException(detail);
}
return body.string();
}
}

public Map<String, Object> call(String api, Map<String, Object> params) throws IOException {
Claims payload = Jwts.claims();
payload.put("method", api);
payload.put("params", params);
String content = this.encodeToken(payload);
String url = BASE_URL + "/" + api;
String token = this.sendRequest(url, content);
Map<String, Object> result = this.decodeToken(token);
return (Map<String, Object>) result.get("result");
}

public static void main(String[] args) throws IOException {
// 艺爪付费项目ID
String projectId = "project-id";
// 艺爪付费项目密钥
String projectSecret = "project-secret-xxxxx-xxxxx-xxxxx";
// 接口名称和参数,可参考具体API页面的说明
String api = "customer.info"; // 接口名称
Map<String, Object> params = Map.of(
"paywall_id", "xxx", // 付费界面ID,ID和别名传一个即可
"paywall_alias", "", // 付费界面别名,ID和别名传一个即可
"customer", Map.of(
"external_id", "test-user-id", // 商户系统用户ID
"nickname", null, // 商户系统用户用户名/昵称,可选
"external_dt_created", null // 商户系统用户创建时间,可选
),
"include_balance", true // 是否返回用户余额
);
EzrevenueClient client = new EzrevenueClient(projectId, projectSecret);
Map<String, Object> result = client.call(api, params);
System.out.println(result);
}
}