简体   繁体   中英

Creating Facebook event with an image via Spring social

Looking over the spring social facebook api, EventOperations offers

    createEvent(java.lang.String name, java.lang.String startTime, 
                                                      java.lang.String endTime)

to post Events to facebook.

Is there a way to post more information for the event, like event image and event description?

Looking at createEvent method, it does not currently support the attachment (event image). It does however call into GraphApi publish to actually publish the event:

public String createEvent(String name, String startTime, String endTime) {
    requireAuthorization();
    MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
    data.set("name", name);
    data.set("start_time", startTime);
    data.set("end_time", endTime);
    return graphApi.publish("me", "events", data);
}

graphApi .publish(...) itself ( which is implemented in FacebookTemplate ) would support anything as a {key,value} pair that Facebook is ready to accept, as it just delegates to the RestTemplate, and feeds all the {key,value} pairs to Facebook through a regular HTTP POST:

public String publish(String objectId, String connectionType, MultiValueMap<String, Object> data) {
    MultiValueMap<String, Object> requestData = new LinkedMultiValueMap<String, Object>(data);
    URI uri = URIBuilder.fromUri(GRAPH_API_URL + objectId + "/" + connectionType).build();
    Map<String, Object> response = getRestTemplate().postForObject(uri, requestData, Map.class);
    return (String) response.get("id");
}

Hence you can extend the EventTemplate and add another createEvent method, that would take an image name and an image file path , and would added it as an additional {key,value} to a data MultiMap:

data.set( "@" + imageName, "@" + imagePath )

Hence the method would look close to:

public String createEvent( String name, 
                           String startTime, 
                           String endTime,
                           String imageName,
                           String imagePath ) {   // or maybe even "File image", where you would derive the path
    requireAuthorization();
    MultiValueMap<String, Object> data = new LinkedMultiValueMap<String, Object>();
    data.set("name", name);
    data.set("start_time", startTime);
    data.set("end_time", endTime);

    data.set( "@" + imageName, "@" + imagePath );  // <<< adding this line

    return graphApi.publish("me", "events", data);
}

This of course does not include any possible validation that you might want to do, etc.. Once you have this working, which seems to be pretty straightforward, you can donate / contribute it back to Spring Social => they'd be very pleased :)

Although answer provided was possibly correct at the time, nowadays this answer is misleading and wrong. Since v2.1 onwards you can't do CREATE/UPDATE/DELETE of Events via API.

https://developers.facebook.com/docs/graph-api/reference/v2.1/event/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM