Facebook Connect Android——使用stream.publish @ http://api.facebook.com/restserver.php

2024-03-05

我收到以下错误:

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

我应该将 contentType 设置为什么?我应该设置为:

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

or

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

这是我用来发布流的方法:

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

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

Thanks.


这是来自Facebook开发者维基:http://wiki.developers.facebook.com/index.php/API http://wiki.developers.facebook.com/index.php/API

Note:如果您手动形成 HTTP POST 请求到 Facebook,您必须 在 POST 中包含请求数据 身体。此外,您还应该包括 内容类型:标题应用程序/x-www-form-urlencoded.

Use 多部分/表单数据仅上传文件时(例如 Facebook API 上的 Photos.upload)

另外,基于此 API 参考,http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application,这就是你做错的事情。

1)不要使用HashMap存储 Facebook 参数。参数必须是sorted通过他们的钥匙。而是使用SortedMap接口及使用TreeMap存储 Facebook 参数。

2) Always包括sig地图中的参数before将通话发送至 Facebook。您收到“104 签名不正确”,因为 Facebook 找不到sig您的请求中的参数。

参考资料 (http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application 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;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Facebook Connect Android——使用stream.publish @ http://api.facebook.com/restserver.php 的相关文章

  • Locale.getDefault().getCountry() 返回空字符串

    我正在尝试使用国家 地区代码获取用户语言 例如en US es es 但是当我使用Locale getDefault getCountry 它返回空字符串 虽然它给了我正确的语言Locale getDefault getLanguage N
  • Android 在启动时启动服务,如何在设备重启后重新启动服务类?

    我需要在启动时启动一项服务 我搜索了很多 他们正在谈论广播接收器 由于我是 Android 开发新手 所以我对 Android 上的服务并没有清楚的了解 请提供一些源代码 您的接收者 public class MyReceiver exte
  • 如何在 Android 上的 HttpPost 中发送 unicode 字符

    我试图在我的应用程序中允许多语言支持 这会发出 HTTP post 来上传新消息 我需要做什么才能支持日语和其他非拉丁语语言 我的代码目前看起来像这样 note the msg string is a JSON message by the
  • 如何在java中使jpeg无损?

    有没有人可以告诉我如何使用编写 jpeg 文件losslessjava中的压缩 我使用下面的代码读取字节来编辑字节 WritableRaster raster image getRaster DataBufferByte buffer Da
  • 打印包含 JBIG2 图像的 PDF

    请推荐一些库 帮助我打印包含 JBIG2 编码图像的 PDF 文件 PDFRenderer PDFBox别帮我 这些库可以打印简单的 PDF 但不能打印包含 JBIG2 图像的 PDF PDFRenderer尝试修复它 根据 PDFRedn
  • 为什么 ConcurrentHashMap::putIfAbsent 比 ConcurrentHashMap::computeIfAbsent 更快?

    使用 ConcurrentHashMap 我发现computeIfAbsent 比putIfAbsent 慢两倍 这是简单的测试 import java util ArrayList import java util List import
  • 覆盖 MATLAB 默认静态 javaclasspath 的最佳方法

    MATLAB 配置为在搜索用户可修改的动态路径之前搜索其静态 java 类路径 不幸的是 静态路径包含相当多非常旧的公共库 因此如果您尝试使用新版本 您可能最终会加载错误的实现并出现错误 例如 静态路径包含 google collectio
  • Facebook - 如何获取永久用户访问令牌[重复]

    这个问题在这里已经有答案了 我的公司有一个公司 Facebook 帐户 他们在不同的相册中发布活动照片 我的要求是从Facebook相册中获取所有照片并显示在我们的公司网站上 我能够获取照片 但用户访问令牌的有效期只有 60 天 这意味着每
  • tomcat 过滤所有 web 应用程序

    问题 我想对所有网络应用程序进行过滤 我创建了一个过滤器来监视对 apache tomcat 服务器的请求 举例来说 它称为 MyFilter 我在 netbeans 中创建了它 它创建了 2 个独立的目录 webpages contain
  • onTouchEvent()中如何区分移动和点击?

    在我的应用程序中 我需要处理移动和单击事件 一次点击是由一个 ACTION DOWN 操作 多个 ACTION MOVE 操作和一个 ACTION UP 操作组成的序列 理论上 如果您收到 ACTION DOWN 事件 然后收到 ACTIO
  • 从 Java 日历迁移到 Joda 日期时间

    以前 当我第一次设计股票应用相关软件时 我决定使用java util Date表示股票的日期 时间信息 后来我体会到了大部分方法java util Date已弃用 因此 很快 我重构了所有代码以利用java util Calendar 然而
  • Apache Commons CLI:替代已弃用的 OptionBuilder?

    IntelliJ 显示此示例代码中不推荐使用 OptionBuilderhttp commons apache org proper commons cli usage html http commons apache org proper
  • Android开发:未定义方法

    大家好 我是 Android 和 Eclipse 的新手 我刚刚遵循了developer android com 上的教程 现在我在添加操作栏 http developer android com training basics actio
  • 如何在keycloak中动态编辑standalone.xml文件

    我正在尝试通过 docker 编辑standalone xml 并尝试添加 但 keycloak 正在使用它standalone xml 但我可以看到standalone xml 文件中的更改 我需要在standalone xml 文件中添
  • ExceptionHandler 不适用于 Throwable

    我们的应用程序是基于 Spring MVC 的 REST 应用程序 我正在尝试使用 ExceptionHandler 注释来处理所有错误和异常 I have ExceptionHandler Throwable class public R
  • 从java中的字符串数组中删除空值

    java中如何从字符串数组中删除空值 String firstArray test1 test2 test4 我需要像这样没有 null 空 值的 firstArray String firstArray test1 test2 test4
  • 从一个文本文件中获取数据并将其移动到新的文本文件

    我有一个文件 里面有数据 在我的主要方法中 我读入文件并关闭文件 我调用另一种方法 在原始文件的同一文件夹内创建一个新文件 所以现在我有两个文件 原始文件和通过我调用的方法生成的文件 我需要另一种方法 从原始文件中获取数据并将其写入创建的新
  • Java 编码风格、局部变量与重复方法调用

    我更喜欢使用局部变量而不是多次调用同一方法 I prefer this Vehicle vehicle person getVehicle if vehicle instanceof Car Car car Car vehicle car
  • gradle-experimental:0.1.0 buildConfigField

    谁知道怎么定义buildConfigField在实验性的 gradle 插件中 android productFlavors create demo applicationId com anthonymandra rawdroid buil
  • Spring Boot MSSQL Kerberos 身份验证

    目前在我的春季靴子中application properties文件中 我指定以下行来连接到 MSSql 服务器 spring datasource url jdbc sqlserver localhost databaseName spr

随机推荐