Search code examples
javascriptfacebookfacebook-graph-apisdkfacebook-canvas

Facebook Javascript API will Publish Album, but not finish image upload


Okay,

So I'm trying to upload an image to a user's profile, using the JavaScript SDK by Facebook. Essentially, I have it working—but not all the way.

It will add an album to my photos using the App Title, which is what I want. However, it won't add the photo to the album provided in the URL parameter. See below:

// Create the album and add the image
FB.api(
    "/me/photos",
    "POST",
{
    "url": "https://[VALID URL]"
},
function (response) {
    if (!response || response.error) {
        // Log the error
        console.log(response.error);
    } else {
        console.log(response);
    }
}

Like I said, something is working, because the album is showing up in my profile. But I get this error returned in the console:

Object {message: "An unknown error has occurred.", type: "OAuthException", code: 1} 

Any help would be greatly appreciated!

UPDATE: I figured it out. See the explanation below.


Solution

  • Wow, gotta love the run-arounds.

    So my problem was not creating an album first, and then posting to that album in the response function. So the final (working) code, looks like this:

    FB.api(
        "/me/albums",
        "POST",
        {
            "name": "[Album Name]",
            "message": "[Test Message]"
        },
        function (response) {
          if (!response || response.error) {
            console.log(response.error);
          } else {
            // Get the album ID we just created
            var albumID = response.id;
            FB.api(
                "/" + albumID + "/photos",
                "POST",
                {
                    message: "Test message",
                    url: "[IMG URL]",
                    no_story: true
                },
                function (response) {
                  if (!response || response.error) {
                    console.log(response.error);
                  } else {
                    console.log(response);
                    console.log(response.id);
                  }
                }
            );
          }
        }
    );
    

    Works like a charm!