Facebook连接的Andr​​oid - 使用stream.publish @ http://api.facebook.com/restserver.php [英] Facebook Connect Android -- using stream.publish @ http://api.facebook.com/restserver.php

查看:111
本文介绍了Facebook连接的Andr​​oid - 使用stream.publish @ http://api.facebook.com/restserver.php的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我收到以下错误:

<error_code>104</error_code>
<error_msg>Incorrect signature</error_msg>

我应该被设置的contentType类型?我应该设置为:

What should I be setting contentType type as? Should I set as:

String contentType = "application/x-www-form-urlencoded";

String contentType = "multipart/form-data; boundary=" + kStringBoundary; 

这是我正在写流:

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();

下面是我使用发布该流的方法:

Here's the method I am using to publish the stream:

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();
     }
}

下面是generatePostBody():

Here is 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();
}

感谢。

推荐答案

这是从的Facebook 开发商维基:的 http://wiki.developers.facebook.com/index.php/API

This is from Facebook Developers Wiki: http://wiki.developers.facebook.com/index.php/API

注意:如果您手动形成你的HTTP
  POST请求的Facebook,你必须
  包括在POST请求数据
  身体。此外,你应该包括
  一个内容类型:报头
  的应用程序/ x-WWW的形式urlen codeD

Note: If you manually form your HTTP POST requests to Facebook, you must include the request data in the POST body. In addition, you should include a Content-Type: header of application/x-www-form-urlencoded.

使用的multipart / form-data的只上传文件时(例如Photos.upload在Facebook API)

Use multipart/form-data when uploading files only (e.g. Photos.upload on Facebook API)

此外,在此基础上API参考,http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application,这是你在​​做什么错。

Also, Based on this API reference, http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application, this is what you're doing wrong.

1)不要使用的HashMap 来存储Facebook的参数。参数必须是整理按他们的关键。而使用的SortedMap 界面并使用 TreeMap的来存储Facebook的参数。

1) Don't use HashMap to store Facebook parameters. Parameters must be sorted by their key. Rather use SortedMap interface and use TreeMap to store Facebook parameters.

2)始终包含地图中的 SIG 参数之前的 发送呼叫到Facebook。你得到一个104错误签名因为Facebook没有找到你请求中的 SIG 参数。

2) Always include the sig parameter in the map before sending the call to Facebook. You're getting a "104 Incorrect signature" because Facebook doesn't find the sig parameter in your request.

参考(http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application)正好说明了如何创建Facebook的使用MD5签名。

The reference (http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application) shows exactly how to create a MD5 signature which facebook uses.

下面是如何快速创建一个Facebook的 SIG 值。

Here's how to quickly create a sig value for 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;

这篇关于Facebook连接的Andr​​oid - 使用stream.publish @ http://api.facebook.com/restserver.php的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆