dro*_*823 5 java android facebook
我收到以下错误:
<error_code>104</error_code>
<error_msg>Incorrect signature</error_msg>
Run Code Online (Sandbox Code Playgroud)
我应该将contentType类型设置为什么?我应该设置为:
String contentType = "application/x-www-form-urlencoded";
Run Code Online (Sandbox Code Playgroud)
要么
String contentType = "multipart/form-data; boundary=" + kStringBoundary;
Run Code Online (Sandbox Code Playgroud)
这就是我写流的方式:
HttpURLConnection conn = null;
OutputStream out = null;
InputStream in = null;
try {
conn = (HttpURLConnection) _loadingURL.openConnection();
conn.setDoOutput(true);
conn.setDoInput(true);
if (method != null) {
conn.setRequestMethod(method);
if ("POST".equals(method)) {
//"application/x-www-form-urlencoded";
String contentType = "multipart/form-data; boundary=" + kStringBoundary;
//String contentType = "application/x-www-form-urlencoded";
conn.setRequestProperty("Content-Type", contentType);
}
// Cookies are used in FBPermissionDialog and FBFeedDialog to
// retrieve logged user
conn.connect();
out = conn.getOutputStream();
if ("POST".equals(method)) {
String body = generatePostBody(postParams);
if (body != null) {
out.write(body.getBytes("UTF-8"));
}
}
in = conn.getInputStream();
Run Code Online (Sandbox Code Playgroud)
这是我用来发布流的方法:
private void publishFeed(String themessage) {
//Intent intent = new Intent(this, FBFeedActivity.class);
// intent.putExtra("userMessagePrompt", themessage);
// intent.putExtra("attachment",
Map<String, String> getParams = new HashMap<String, String>();
// getParams.put("display", "touch");
// getParams.put("callback", "fbconnect://success");
// getParams.put("cancel", "fbconnect://cancel");
Map<String, String> postParams = new HashMap<String, String>();
postParams.put("api_key", _session.getApiKey());
postParams.put("method", "stream.publish");
postParams.put("session_key", _session.getSessionKey());
postParams.put("user_message", "TESTING 123");
// postParams.put("preview", "1");
postParams.put("attachment", "{\"name\":\"Facebook Connect for Android\",\"href\":\"http://code.google.com/p/fbconnect-android/\",\"caption\":\"Caption\",\"description\":\"Description\",\"media\":[{\"type\":\"image\",\"src\":\"http://img40.yfrog.com/img40/5914/iphoneconnectbtn.jpg\",\"href\":\"http://developers.facebook.com/connect.php?tab=iphone/\"}],\"properties\":{\"another link\":{\"text\":\"Facebook home page\",\"href\":\"http://www.facebook.com\"}}}");
// postParams.put("user_message_prompt", "22222");
try {
loadURL("http://api.facebook.com/restserver.php", "POST", getParams, postParams);
} catch (MalformedURLException e) {
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
这是generatePostBody():
private String generatePostBody(Map<String, String> params) {
StringBuilder body = new StringBuilder();
StringBuilder endLine = new StringBuilder("\r\n--").append(kStringBoundary).append("\r\n");
body.append("--").append(kStringBoundary).append("\r\n");
for (Entry<String, String> entry : params.entrySet()) {
body.append("Content-Disposition: form-data; name=\"").append(entry.getKey()).append("\"\r\n\r\n");
String value = entry.getValue();
if ("user_message_prompt".equals(entry.getKey())) {
body.append(value);
}
else {
body.append(CcUtil.encode(value));
}
body.append(endLine);
}
return body.toString();
}
Run Code Online (Sandbox Code Playgroud)
谢谢.
这是来自Facebook开发者 Wiki: http: //wiki.developers.facebook.com/index.php/API
注意:如果您手动向 Facebook 发起 HTTP POST 请求,则必须在 POST 正文中包含请求数据。此外,您还应该包含 application/x-www-form-urlencoded的 Content-Type: 标头。
仅在上传文件时使用multipart/form-data(例如 Facebook API 上的 Photos.upload)
另外,根据这个API参考,http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application,这就是你做错的事情。
1) 不要用来HashMap存储Facebook参数。参数必须sorted由它们的键指定。而是使用SortedMap接口并用于TreeMap存储 Facebook 参数。
2)在将呼叫发送到 Facebook之前,始终将参数包含sig在地图中。您收到“104 签名不正确”是因为 Facebook在您的请求中找不到该参数。sig
参考资料 ( http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application ) 准确显示了如何创建 facebook 使用的 MD5 签名。
以下是如何快速sig为 Facebook 创造价值。
String hashString = "";
Map<String, String> sortedMap = null;
if (parameters instanceof TreeMap) {
sortedMap = (TreeMap<String, String>) parameters;
} else {
sortedMap = new TreeMap<String, String>(parameters);
}
try {
Iterator<String> iter = sortedMap.keySet().iterator();
StringBuilder sb = new StringBuilder();
synchronized (iter) {
while (iter.hasNext()) {
String key = iter.next();
sb.append(key);
sb.append("=");
String value = sortedMap.get(key);
sb.append(value == null ? "" : value);
}
}
sb.append(secret);
MessageDigest digest = MessageDigest.getInstance("MD5");
byte[] digested = digest.digest(sb.toString().getBytes());
BigInteger bigInt = new BigInteger(1, digested);
hashString = bigInt.toString(16);
while (hashString.length() < 32) {
hashString = "0" + hashString;
}
} catch (NoSuchAlgorithmException nsae) {
// TODO: handle exception
logger.error(e.getLocalizedMessage(), e);
}
return hashString;
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
4794 次 |
| 最近记录: |