【发布时间】:2016-12-13 14:24:58
【问题描述】:
我无法为我的应用的两个非好友用户获取共同好友。
根据 all_mutual_friends 权限,我需要连同 appsecret_proof 参数一起发出请求。
我使用这个 GET 调用生成了 app_access_token:
GET /oauth/access_token
?client_id={app-id}
&client_secret={app-secret}
&grant_type=client_credentials
我已经三次检查了 app_id 和 app_secret,它们是正确的。我通过 SHA256 用 Java 中的 app_secret 对 app_access_token 进行哈希处理生成了 appsecret_proof。
现在,当我请求共同的朋友(发送 appsecret_proof 作为查询参数)时,它会回复说
"Invalid appsecret_proof provided in the API argument"
带有 GraphMethodException。原始请求(没有 appsecret_proof)对于朋友用户来说工作正常。这里有什么指点吗?
这是我用来生成 appsecret_proof 的 java 代码:
public static String hashMac(String text, String secretKey)
throws SignatureException {
try {
Key sk = new SecretKeySpec(secretKey.getBytes(), HASH_ALGORITHM);
Mac mac = Mac.getInstance(sk.getAlgorithm());
mac.init(sk);
final byte[] hmac = mac.doFinal(text.getBytes());
return toHexString(hmac);
} catch (NoSuchAlgorithmException e1) {// throw an exception or pick a different encryption method
throw new SignatureException(
"error building signature, no such algorithm in device "
+ HASH_ALGORITHM);
} catch (InvalidKeyException e) {
throw new SignatureException(
"error building signature, invalid key " + HASH_ALGORITHM);
}
}
private static final String HASH_ALGORITHM = "HmacSHA256";
public static String toHexString(byte[] bytes) {
StringBuilder sb = new StringBuilder(bytes.length * 2);
Formatter formatter = new Formatter(sb);
for (byte b : bytes) {
formatter.format("%02x", b);
}
return sb.toString();
}
我的服务器是基于 python 的。
【问题讨论】:
标签: java facebook facebook-graph-api