Domanda

Sto tentando di utilizzare il seguente codice di caricare una foto su Facebook utilizzando l'API grafico. Continuo a ricevere "Bad Request", ma non so perché. Posso caricare la foto appena benissimo con riccio con gli stessi parametri. Sto usando Java con HttpClient.

    PostMethod filePost = new PostMethod('https://graph.facebook.com/me/photos');
    filePost.setParameter('access_token', 'my-access-token')
    filePost.setParameter('message', 'test image')

    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
    try {
      println("Uploading " + file.getName() + " to 'https://graph.facebook.com/me/photos'");
      Part[] parts = [new FilePart('source', file.getName(), file)]
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      HttpClient client = new HttpClient();
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      int status = client.executeMethod(filePost);
      if (status == HttpStatus.SC_OK) {
        println(
                "Upload complete, response=" + filePost.getResponseBodyAsString()
        );
      } else {
        println(
                "Upload failed, response=" + HttpStatus.getStatusText(status)
        );
      }
    } catch (Exception ex) {
      println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
      ex.printStackTrace();
    } finally {
      filePost.releaseConnection();
    }

UPDATE: Altro per questo. Ho preso un po 'di informazioni la risposta e sto ottenendo questo:

{ "errore": { "type": "OAuthException", "messaggio": "Un token di accesso attivo deve essere usato per chiedere informazioni circa l'utente corrente"}}

Ma che non sembra proprio come sto usando il token di accesso che Facebook restituisce a me dopo il processo di autorizzare.

Facendo codice ricciolo:

curl -F 'access_token=my-access-token' -F 'source=@/path/to/image.jpg' -F 'message=Some caption' https://graph.facebook.com/me/photos
È stato utile?

Soluzione

ho risolto il problema. Invece di aggiungere i params al PostMethod, avevo bisogno di aggiungere l'access_token e il messaggio alla matrice Parte []. codice completo:

    PostMethod filePost = new PostMethod('https://graph.facebook.com/me/photos');
    filePost.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
    try {
      println("Uploading " + file.getName() + " to 'https://graph.facebook.com/me/photos'");
      Part[] parts = [new FilePart('source', file.getName(), file), new StringPart('access_token', "${facebookData.access_token}"), new StringPart('message', 'some message')]
      filePost.setRequestEntity(new MultipartRequestEntity(parts, filePost.getParams()));
      HttpClient client = new HttpClient();
      client.getHttpConnectionManager().getParams().setConnectionTimeout(5000);
      int status = client.executeMethod(filePost);
      if (status == HttpStatus.SC_OK) {
        println("Upload complete, response=" + filePost.getResponseBodyAsString());
      } else {
        println("Upload failed, response=" + HttpStatus.getStatusText(status));
        // Create response
        StringBuilder notificationsSendResponse = new StringBuilder();
        byte[] byteArrayNotifications = new byte[4096];
        for (int n; (n = filePost.getResponseBodyAsStream().read(byteArrayNotifications)) != -1;) {
          notificationsSendResponse.append(new String(byteArrayNotifications, 0, n));
        }
        String notificationInfo = notificationsSendResponse.toString();
      }
    } catch (Exception ex) {
      println("ERROR: " + ex.getClass().getName() + " " + ex.getMessage());
      ex.printStackTrace();
    } finally {
      filePost.releaseConnection();
    }

Altri suggerimenti

È possibile utilizzare l'API Java socialauth caricamento delle immagini tramite applicazioni web.

http://code.google.com/p/socialauth/

Questa è la versione Android del metodo

  private void postOnFacebook() {
        try {
            HttpPost httpPost = new HttpPost("https://graph.facebook.com/me/photos");
            MultipartEntity entity = new MultipartEntity();
            String base64Image = "iVBORw0KGgoAAAANSUhEUgAAAAUAAAAFCAIAAAACDbGyAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAAASSURBVBhXY3gro4KMKOPLqAAAq/UdZuRmLacAAAAASUVORK5CYII=";
            byte[] imageData = Base64.decode(base64Image, 0);
            entity.addPart("access_token", new StringBody("your access token"));
            entity.addPart("message", new StringBody("test msg"));
            entity.addPart("source", new ByteArrayBody(imageData, "test"));
            CloseableHttpClient httpclient = HttpClientBuilder.create().build();
            httpPost.getParams().setBooleanParameter(USE_EXPECT_CONTINUE, false);
            httpPost.setEntity(entity);
            HttpResponse resp = httpclient.execute(httpPost);
            HttpEntity entity2 = resp.getEntity();
            if (entity != null) {
                String responseBody = EntityUtils.toString(entity2);
                responseBody.toString();
            }
        } catch (Exception ex) {

            ex.printStackTrace();
        }
    }
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top