package com.adx.tencent.oppo.service; import java.nio.charset.StandardCharsets; import java.security.MessageDigest; import java.time.Instant; import java.util.Base64; /** OPPO ownerToken 本地生成服务。 */ public class OppoAuthService { public String getOwnerToken(String ownerId, String apiId, String apiKey) { if (ownerId == null || ownerId.isBlank() || apiId == null || apiId.isBlank() || apiKey == null || apiKey.isBlank()) { throw new IllegalStateException("OPPO ownerId/apiId/apiKey are required"); } long now = Instant.now().getEpochSecond(); String sign = sha1(apiId + apiKey + now); return Base64.getEncoder().encodeToString( (ownerId + "," + apiId + "," + now + "," + sign).getBytes(StandardCharsets.UTF_8)); } private static String sha1(String value) { try { byte[] digest = MessageDigest.getInstance("SHA-1").digest(value.getBytes(StandardCharsets.UTF_8)); StringBuilder result = new StringBuilder(); for (byte b : digest) result.append(String.format("%02x", b)); return result.toString(); } catch (Exception e) { throw new IllegalStateException("OPPO token sign failed", e); } } }