Facebook like button in WebView with SDK
Asked Answered
F

2

8

I'm trying to implement Facebook Like button which is not part of Android Facebook SDK using WebView. The idea is very simple. I use SDK to log into user account using SSO so user don't need to type login/password again if user is already logged in android FB app. Then I want to use WebView to insert standard Like Button.
I already have user auth token, permission for sending status on the wall etc. The problem is how to tell WebView that user is already sign-in. I was trying to use WebView (with enabled JS) with this URL (webview.loadURL()) generated by FB:

http://www.facebook.com/plugins/like.php?href=myurl&send=false&layout=button_count&width=450&show_faces=true&action=like&colorscheme=light&font&height=21&appId=myId"
            + "&token=" + mFacebook.getAccessToken()+"&expires="+mFacebook.getAccessExpires(); //(or auth_token instead)<br>

Obviously this is wrong/or is not enought to send autorization in this way because after click on Like button user is redirected to login page in web browser.
So the question is how to edit this URL or how to set cookies (what to set to URL in CookieManager and which cookies) in WebView to sign user in.
Thanks for any help!

Filth answered 17/9, 2012 at 17:47 Comment(2)
too old question, but if somebody still need to fix this, you may use my library, read here: https://mcmap.net/q/664379/-how-to-add-to-my-android-application-a-button-than-do-like-to-a-facebook-pageByyourleave
possible duplicate of android facebook likeSpoil
E
1

I'm not quite sure about FB app access token being valid for using the web api, but let's try something.

First, make sure you're actually using cookies for your WebView instance:

CookieManager.getInstance().setAcceptCookies(true);

I'm not sure whether the facebook redirect page will try to set cookies, so try this and see if it does the trick:

webview.setWebViewClient(new WebViewClient() {  
  @Override  
  public boolean shouldOverrideUrlLoading(WebView view, String url)  
  {  
    view.loadUrl(url);  
    return true;
  }  
});

This will force your WebView to open links within itself, so cookies - if any - won't be lost in case the like page issues a redirect.

If problem still persists you can also try setting cookies manually by executing this before you load the url in your WebView:

// This just initializes the sync manager, do it once
CookieSyncManager.createInstance(this);

CookieManager.getInstance().setCookie("facebook.com", "token="
        + mFacebook.getAccessToken() + "; domain=facebook.com");
CookieSyncManager.getInstance().sync();
Extramural answered 22/9, 2012 at 17:8 Comment(9)
It's unfortunately not working. What I can see is that in WebView are different set of cookies (different names) if I log using WebView into FB. So the token/auth_token (I don't know which should be correct, but tried both) is not used or not checked by FB.Filth
Oh, then it must be that web api for FB intentionnally has a separate auth procedure... Try looking for opensource projects integrated with FB and see how they work this particular issue.Extramural
That is why, I'm asking here. I didn't find any why can handle Like button. Now I'm trying to do whole process inside WebView, so the user will have to write down his login/password.Filth
Yeah, it's a shame we weren't able to make it work that way. Great question though. I have very much on my plate right now, but I'll get back to this later, it kinda considers the project i'm working on right now, too. Let me know if you're able to solve it through cookies somehow.Extramural
Now I'm trying to solve this problem using Open Graph API and my own Like Button. But I ran into similar problem: #12656162Filth
In the end cookies works good, just had to create my own WebClient to redirect FB login page to another activity etc. to make it look better. Problem is that sometimes I got 404 or timeout from FB. User has to write down user/login and it's not very user friendly.Filth
so, you've been able to inject your access token as a cookie? I mean, you auth with FB through application api and then use the token you got to auth with web api - transparently for the user?Extramural
No that way. I only save cookies set during FB login in WebView and then use these cookies in all my WebViews (I save them in SharedPreference). So the user log into my app only once. But the user experience with logging using WebView is terrible :-(Filth
I think I get it finally working. See the other question which I sent earlier. Now I have SSO login and own Like button. But you have to do some other requests like if the URL is already liked by user calling some FQL on that link etc.Filth
B
0

I think this is what you are looking for: https://developers.facebook.com/docs/authentication/server-side/

If I'm reading this correctly, once you implement OAuth, you can use the Graph API to implement the "Like" button you are looking for because you can authenticate the device - not just the user.

Billyebilobate answered 24/9, 2012 at 19:11 Comment(1)
That is not exactly answer to my question. What are you suggesting is maybe possible way, but it is still different approach. It's described here: developers.facebook.com/docs/opengraph/actions/builtin/likes. I want to use current Like button with all it's features but not to do it from the start.Filth

© 2022 - 2024 — McMap. All rights reserved.