Facebook like button in WebView with SDK

别来无恙 提交于 2019-12-19 16:21:30

问题


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!


回答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();



回答2:


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.



来源:https://stackoverflow.com/questions/12464570/facebook-like-button-in-webview-with-sdk

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!