I'm writing an android application that uses the Facebook SDK for Android. This SDK uses a Session object to keep track of log ins, but I need to either retrieve or find a stored Facebook log in cookie.
Basically, I've hit a point where I need to get larger images that those supplied directly by the Graph API responses. I've tried to use https://graph.facebook.com/{picture-id}/picture?width=300&access_token={access_token}, however, this behaves sporadically in returning images and often returns the gray question mark instead.
So I found a workaround: use the link data that Graph API supplies, for example, (https://www.facebook.com/photo.php?fbid=790392014355716&set=at.445939865467601.103307.100001547758266.604854208&type=1&relevant_count=1). The solution is to navigate to the link, then parse the HTML for the image tag that has the id "fbPhotoImage" and scrape it's src attribute.
Now in theory this works each and every time, but there's a slight problem. I need to use the Facebook log in cookie to get the correct HTML response, otherwise the sought-after image tag is not returned.
Here's a code snippet that I've been using (includes the Jsoup library):
public static String getFacebookPhotoLinkFromLink(String link) throws IOException{
// This function parses the HTML returned in the link data for the link to the photo file
// set the user-agent to mimic a desktop browser
String user_agent = "Mozilla/5.0 (Windows NT 6.3; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/37.0.2049.0 Safari/537.36";
// get the facebook cookie
CookieManager cookieManager = CookieManager.getInstance();
String cookie = cookieManager.getCookie("https://facebook.com");
// make the connection and retrieve the html
Connection connection = Jsoup.connect(link)
.userAgent(user_agent)
.cookie("SESSIONID", cookie);
Document doc = connection.get();
// get all img elements
Elements imgElements = doc.getElementsByTag("img");
for (Element imgElement : imgElements){
// get the correct image element
if (imgElement.attr("id").equals("fbPhotoImage")){
String imgURL = imgElement.absUrl("src");
Log.e(TAG, imgURL);
return imgURL;
}
}
return null;
}
This function should return the correct link to the raw image, but instead it returns null because the cookie is set incorrectly.
How can I find this cookie?
Regards, RF
0 comments:
Post a Comment