Hello experts ! I am trying hard for many days to implement authentication facility in my app by facebuk login id. I tried with fbconnect api but it wasnt working. I got this code and tried . It works fine on version 1.6 and connects to my facebook application. But i just need to ensure if login is successful then it should return to my local app activity.
if it feels odd understanding the code then here is the link ... http://forum.developers.facebook.com/viewtopic.php?pid=146956 I am still a beginner and takes a little while to understand code :S up to my understanding there is a class fConnect created by author which make http posts requests to facebook api. Confusions: 1) why is xd_receiver link is required ? its imp to understand because its used in the onPagestarted() function in facebookwebview client class in Connection.java. There is an if condition for page.getPath().equals("/connect/xd_receiver.htm" & when its true a session is created and Intent is called for the intended activity in the application. Thats what i want to do. Problem: there is no getsetting() function implemented in the FConnect class :S if the author has done it i might have been able to run it correctly. Kindly guide me about working of this function so that i can implement it myself. Creating a User Class Code: public class User { public String uid; public String name; public String picUrl; } Of course you can add some fields here, but I didn't have any use of the others. 2°) Creating the WebView facebook_connect.xml Code: <?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="#ffffff" android:scrollbars="none"> <WebView android:id="@+id/facebookview" android:layout_width="fill_parent" android:layout_height="fill_parent" /> </ScrollView> Connection.java Code: public class Connection extends Activity { FConnect fConnect; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.facebook_connect); Toast.makeText(getBaseContext(), R.string.allow_facebook, Toast.LENGTH_LONG).show(); WebView webview; webview = (WebView) findViewById(R.id.facebookview); webview.getSettings().setJavaScriptEnabled(true); webview.setWebViewClient(new FacebookWebViewClient()); fConnect = new FConnect(this); webview.loadUrl("http://www.connect.facebook.com/login.php? return_session=1&nochrome=1&fbconnect=1&extern=2&connect_display=popup&api_key="+FConnect.API_KEY +"&v=1.0&next="+FConnect.XD_RECEIVER+"%3Ffb_login%26fname %3D_opener&cancel_url="+FConnect.XD_RECEIVER+"%23fname%3D_opener %26%257B%2522t%2522%253A3%252C%2522h%2522%253A%2522fbCancelLogin %2522%252C%2522sid%2522%253A %25220.741%2522%257D&channel_url="+FConnect.XD_RECEIVER); } private class FacebookWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { super.onPageStarted(view, url, favicon); try { String subUrl = URLDecoder.decode(url).trim(); URL page = new URL(subUrl); if (page.getPath().equals("/connect/xd_receiver.htm")) { subUrl = subUrl.substring(subUrl.indexOf("{") + 1, subUrl.indexOf("}")); fConnect.createSession(subUrl); List<User> user = fConnect.getUserInfo(fConnect.getSetting("uid")); Toast.makeText(getBaseContext(), "Welcome, "+user.get(0).name+", you're now connected !", Toast.LENGTH_LONG).show(); Intent intent = new Intent(getBaseContext(), Base.class); //The next Activity to call startActivity(intent); finish(); } } catch (Exception e) { Toast.makeText(getBaseContext(), R.string.url_parsing, Toast.LENGTH_LONG).show(); } } } } 3°) Creating a FConnect Class Code: public class FConnect { static public final String SECRET = "your secret key"; static public final String API_KEY = "your api key"; static public final String XD_RECEIVER = "http:// link to your xd receiver.htm file"; private final String v = "1.0"; private final String format = "JSON"; private final String server = "http://api.facebook.com/ restserver.php"; static public final String PREF_NAME = "Your application name"; static public final String[] PREF_VALUES = {"session_key", "uid", "expires", "secret", "sig"}; private SharedPreferences settings; Context ctx; public FConnect(Context context) { ctx = context; settings = context.getSharedPreferences(PREF_NAME, 0); } //Getting the GET information after logging-in Facebook Connect //Creating session_key, uid, expires, secret, sig on the shared parameters public void createSession(String query) throws ClientProtocolException { String[] subQuery = query.split(","); SharedPreferences.Editor editor = settings.edit(); if (subQuery.length != 5) throw new ClientProtocolException("Cannot generate session, only "+subQuery.length+" values"); for (int i = 0; i < subQuery.length; i++) { String[] values = subQuery[i].split(":"); for (int j = 0; j < 2; j++) { values[j] = values[j].replace('"', ' '); values[j] = values[j].trim(); } editor.putString(values[0], values[1]); } editor.commit(); } //Login off the user public void killSession() { SharedPreferences.Editor editor = settings.edit(); for (int i = 0; i < PREF_VALUES.length; i++) editor.remove(PREF_VALUES[i]); editor.commit(); } //Does the Session's still valid? public boolean isSession() throws ClientProtocolException, IOException, NoSuchAlgorithmException { if (getSetting("session_key") != "") { StringBuilder params = new StringBuilder(); params.append("api_key=" + API_KEY); params.append("&call_id=" + System.currentTimeMillis()); params.append("&format=" + format); params.append("&method=Users.isAppUser"); params.append("&session_key=" + getSetting("session_key")); params.append("&v=" + v); params.append("&sig=" + getSig(params, SECRET)); String result = postRequest(params.toString()); return Boolean.parseBoolean(result); } return (false); } //Get the uids //Return a list of users's informations, usable with users.get(#).picUrl for example public List<User> getUserInfo(String uids) throws ClientProtocolException, IOException, NoSuchAlgorithmException, JSONException { String fields = "name, pic"; StringBuilder params = new StringBuilder(); params.append("api_key=" + API_KEY); params.append("&call_id=" + System.currentTimeMillis()); params.append("&fields=" + fields); params.append("&format=" + format); params.append("&method=Users.getInfo"); params.append("&session_key=" + getSetting("session_key")); params.append("&uids=" + uids); params.append("&v=" + v); params.append("&sig=" + getSig(params, SECRET)); String result = postRequest(params.toString()); JSONArray users = new JSONArray(result); List<User> r = new ArrayList<User>(); for (int i = 0; i < users.length(); i++) { JSONObject u = users.getJSONObject(i); User user = new User(); user.name = u.getString("name"); user.picUrl = u.getString("pic"); r.add(user); } return r; } private String getSig(StringBuilder params, String s) throws NoSuchAlgorithmException, UnsupportedEncodingException { return SimpleMD5.MD5(params.toString().replaceAll("&", "") + s); } private String postRequest(String params) throws ClientProtocolException, IOException { HttpClient client = new DefaultHttpClient(); HttpPost post = new HttpPost(server); post.setHeader(HTTP.CONTENT_TYPE, "application/x-www-form- urlencoded"); post.setEntity(new StringEntity(params)); HttpResponse response = client.execute(post); if (response.getStatusLine().getStatusCode() == 200) { return EntityUtils.toString(response.getEntity()); } throw new IOException(response.getStatusLine().getReasonPhrase()); } } 4°) Creating a SimpleMD5 Class Code: public class SimpleMD5 { static String convertToHex(byte[] data) { StringBuffer buf = new StringBuffer(); for (int i = 0; i < data.length; i++) { int halfbyte = (data[i] >>> 4) & 0x0F; int two_halfs = 0; do { if ((0 <= halfbyte) && (halfbyte <= 9)) buf.append((char) ('0' + halfbyte)); else buf.append((char) ('a' + (halfbyte - 10))); halfbyte = data[i] & 0x0F; } while (two_halfs++ < 1); } return buf.toString(); } public static String MD5(String text) throws NoSuchAlgorithmException, UnsupportedEncodingException { MessageDigest md; md = MessageDigest.getInstance("MD5"); byte[] md5hash = new byte[32]; md.update(text.getBytes("iso-8859-1"), 0, text.length()); md5hash = md.digest(); return convertToHex(md5hash); } } thanks in advance ... regards, wahib -- You received this message because you are subscribed to the Google Groups "Android Developers" group. To post to this group, send email to android-developers@googlegroups.com To unsubscribe from this group, send email to android-developers+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-developers?hl=en To unsubscribe, reply using "remove me" as the subject.