2010-01-26 6 views
5

를 사용하여 나는 다음과 같은 오류는 무엇입니까?페이스 북에 연결 안드로이드 stream.publish @ http://api.facebook.com/restserver.php

:

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

여기에 내가 스트림을 게시 사용하고있는 방법은 다음과 같습니다

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

또는

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

이 내가 스트림을 쓰고있는 방법입니다해야 내가로 설정

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

감사합니다.

+0

'loadURL()'메소드는 어떻게 구현 되나요? 추신. 게시물을 다시 편집하십시오. 여기에 일부 코드를 읽을 수 없습니다 .... –

답변

0

페이스 북 개발자 위키에서이된다 (페이스 북 API에 대한 예 Photos.upload) 파일 만 또한

을 업로드 할 때 http://wiki.developers.facebook.com/index.php/API

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.

사용 다중/폼 데이터를에 기반 이 API 참조, http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application, 이것이 잘못된 것입니다.

1) HashMap을 사용하여 Facebook 매개 변수를 저장하지 마십시오. 매개 변수는 키로 sorted이어야합니다. 오히려 SortedMap 인터페이스를 사용하고 TreeMap을 사용하여 Facebook 매개 변수를 저장하십시오.

2) 항상 페이스 북에 대한 호출을 전송 전에지도 에서 sig 매개 변수를 포함시킵니다. 귀하의 요청에 Facebook이 sig 매개 변수를 찾지 못해서 "104 Incorrect signature"가 표시됩니다.

참조 (http://wiki.developers.facebook.com/index.php/How_Facebook_Authenticates_Your_Application)는 페이스 북에서 사용하는 MD5 서명을 만드는 방법을 정확하게 보여줍니다.

다음은 Facebook 용으로 sig 값을 빠르게 만드는 방법입니다.

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; 
관련 문제