实施 Facebook 营销的正确方法 API 创建新的 Feed 调用?
Correct way to implement Facebook Marketing API create new feed call?
我正在尝试通过 Facebook 营销 API 创建新的数据源。虽然我已经能够创建一个新的提要,但出于某种原因,我的 URL 和 Schedule 参数没有传递给 Facebook,只有提要的名称。
代码
public static String createNewFeed(String catalogId) throws IOException {
HttpSession session = SessionUtils.getSession();
String token = (String) session.getAttribute("FacebookAuthToken");
URL url = new URL("https://graph.facebook.com/" + catalogId + "/product_feeds?name=WalkerWorks_Product_Feed"
+ "&url=https://www.walkerworks.me/feed.xhtml?user=" + session.getAttribute("dbId")
+ "&interval=hourly&hour=1&access_token=" + token);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
BufferedReader in;
StringBuffer response = null;
String input;
// Creating a BufferedReader and StringBuffer to read connection response.
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
response = new StringBuffer();
// While there is more information to be parsed, append it to the response
// StringBuffer.
while ((input = in.readLine()) != null) {
response.append(input);
}
// Close the connection and parse the StringBuffer.
in.close();
Gson gson = new Gson();
FacebookUserIDModel model = gson.fromJson(response.toString(), FacebookUserIDModel.class);
System.out.println("Response: " + response.toString());
return model.getId();
}
显然我在 URL 中有参数,我正在发送到 Facebook。我想知道我是否正确格式化或创建了计划对象。我认为这是我唯一可能出错的地方,因为其他调用在我发送的完全相同的 url 下正常工作。
Facebook 表示这是通话要求:
Facebook Marketing API / Feed API Call
这是创建的提要。产品未上传,我设置的时间表未创建:
Dynamic Feed Creation on Facebook
如有任何帮助,我们将不胜感激。感谢阅读。
原来我需要在请求中将时间表作为 json 对象提交。这个答案功能齐全。
public static String createNewFeed(String catalogId) throws IOException {
HttpSession session = SessionUtils.getSession();
String token = (String) session.getAttribute("FacebookAuthToken");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://graph.facebook.com/" + catalogId + "/product_feeds?name=WalkerWorks_Product_Feed"
+ "&access_token=" + token);
StringEntity params = new StringEntity("schedule={\"url\":\"https://www.walkerworks.me/feed.xhtml?user=" + session.getAttribute("dbId") + "\","
+ "\"interval\":\"hourly\"}");
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);
System.out.println(EntityUtils.toString(request.getEntity()));
HttpResponse response = httpClient.execute(request);
/*
* URL url = new URL("https://graph.facebook.com/" + catalogId +
* "/product_feeds?name=WalkerWorks_Product_Feed" +
* "&url=https://www.walkerworks.me/feed.xhtml?user=" +
* session.getAttribute("dbId") + "&interval=hourly&hour=1&access_token=" +
* token);
*
* HttpURLConnection connection = (HttpURLConnection) url.openConnection();
* connection.setRequestProperty("Content-Type", "application/json; utf-8");
* connection.setRequestMethod("POST"); connection.setDoOutput(true);
* connection.connect();
*/
/*
* BufferedReader in; StringBuffer response = null; String input;
*
* // Creating a BufferedReader and StringBuffer to read connection response. in
* = new BufferedReader(new InputStreamReader(connection.getInputStream()));
* response = new StringBuffer();
*
* // While there is more information to be parsed, append it to the response //
* StringBuffer. while ((input = in.readLine()) != null) {
*
* response.append(input);
*
* }
*/
/*
* // Close the connection and parse the StringBuffer. in.close();
*/
Gson gson = new Gson();
System.out.println("Response: " + response.toString());
FacebookUserIDModel model = gson.fromJson(EntityUtils.toString(response.getEntity()), FacebookUserIDModel.class);
return model.getId();
}
我正在尝试通过 Facebook 营销 API 创建新的数据源。虽然我已经能够创建一个新的提要,但出于某种原因,我的 URL 和 Schedule 参数没有传递给 Facebook,只有提要的名称。
代码
public static String createNewFeed(String catalogId) throws IOException {
HttpSession session = SessionUtils.getSession();
String token = (String) session.getAttribute("FacebookAuthToken");
URL url = new URL("https://graph.facebook.com/" + catalogId + "/product_feeds?name=WalkerWorks_Product_Feed"
+ "&url=https://www.walkerworks.me/feed.xhtml?user=" + session.getAttribute("dbId")
+ "&interval=hourly&hour=1&access_token=" + token);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setRequestMethod("POST");
connection.connect();
BufferedReader in;
StringBuffer response = null;
String input;
// Creating a BufferedReader and StringBuffer to read connection response.
in = new BufferedReader(new InputStreamReader(connection.getInputStream()));
response = new StringBuffer();
// While there is more information to be parsed, append it to the response
// StringBuffer.
while ((input = in.readLine()) != null) {
response.append(input);
}
// Close the connection and parse the StringBuffer.
in.close();
Gson gson = new Gson();
FacebookUserIDModel model = gson.fromJson(response.toString(), FacebookUserIDModel.class);
System.out.println("Response: " + response.toString());
return model.getId();
}
显然我在 URL 中有参数,我正在发送到 Facebook。我想知道我是否正确格式化或创建了计划对象。我认为这是我唯一可能出错的地方,因为其他调用在我发送的完全相同的 url 下正常工作。
Facebook 表示这是通话要求:
Facebook Marketing API / Feed API Call
这是创建的提要。产品未上传,我设置的时间表未创建:
Dynamic Feed Creation on Facebook
如有任何帮助,我们将不胜感激。感谢阅读。
原来我需要在请求中将时间表作为 json 对象提交。这个答案功能齐全。
public static String createNewFeed(String catalogId) throws IOException {
HttpSession session = SessionUtils.getSession();
String token = (String) session.getAttribute("FacebookAuthToken");
HttpClient httpClient = HttpClientBuilder.create().build();
HttpPost request = new HttpPost("https://graph.facebook.com/" + catalogId + "/product_feeds?name=WalkerWorks_Product_Feed"
+ "&access_token=" + token);
StringEntity params = new StringEntity("schedule={\"url\":\"https://www.walkerworks.me/feed.xhtml?user=" + session.getAttribute("dbId") + "\","
+ "\"interval\":\"hourly\"}");
request.addHeader("content-type", "application/x-www-form-urlencoded");
request.setEntity(params);
System.out.println(EntityUtils.toString(request.getEntity()));
HttpResponse response = httpClient.execute(request);
/*
* URL url = new URL("https://graph.facebook.com/" + catalogId +
* "/product_feeds?name=WalkerWorks_Product_Feed" +
* "&url=https://www.walkerworks.me/feed.xhtml?user=" +
* session.getAttribute("dbId") + "&interval=hourly&hour=1&access_token=" +
* token);
*
* HttpURLConnection connection = (HttpURLConnection) url.openConnection();
* connection.setRequestProperty("Content-Type", "application/json; utf-8");
* connection.setRequestMethod("POST"); connection.setDoOutput(true);
* connection.connect();
*/
/*
* BufferedReader in; StringBuffer response = null; String input;
*
* // Creating a BufferedReader and StringBuffer to read connection response. in
* = new BufferedReader(new InputStreamReader(connection.getInputStream()));
* response = new StringBuffer();
*
* // While there is more information to be parsed, append it to the response //
* StringBuffer. while ((input = in.readLine()) != null) {
*
* response.append(input);
*
* }
*/
/*
* // Close the connection and parse the StringBuffer. in.close();
*/
Gson gson = new Gson();
System.out.println("Response: " + response.toString());
FacebookUserIDModel model = gson.fromJson(EntityUtils.toString(response.getEntity()), FacebookUserIDModel.class);
return model.getId();
}