简体   繁体   English

使用SDK的WebView中的Facebook赞按钮

[英]Facebook like button in WebView with SDK

I'm trying to implement Facebook Like button which is not part of Android Facebook SDK using WebView. 我正在尝试使用WebView实现不属于Android Facebook SDK的Facebook Like按钮。 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. 我使用SDK使用SSO登录到用户帐户,因此如果用户已经在android FB应用程序中登录,则无需再次输入登录名/密码。 Then I want to use WebView to insert standard Like Button. 然后,我想使用WebView插入标准的“赞”按钮。
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. 我已经具有用户身份验证令牌,在墙上发送状态的权限等。问题是如何告诉WebView用户已经登录。 I was trying to use WebView (with enabled JS) with this URL (webview.loadURL()) generated by FB: 我试图通过FB生成的URL(webview.loadURL())使用WebView(已启用JS):

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. 显然,这是错误的/或不足以通过这种方式发送授权,因为在单击“赞”按钮后,用户被重定向到Web浏览器中的登录页面。
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. 因此,问题在于如何编辑此URL或如何在WebView中设置Cookie(在CookieManager中设置为URL的内容以及哪些cookie)来登录用户。
Thanks for any help! 谢谢你的帮助!

I'm not quite sure about FB app access token being valid for using the web api, but let's try something. 我不太确定FB应用程序访问令牌对于使用Web API是否有效,但是让我们尝试一下。

First, make sure you're actually using cookies for your WebView instance: 首先,请确保您实际上在为WebView实例使用cookie:

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: 我不确定Facebook重定向页面是否会尝试设置cookie,因此请尝试以下操作,看看是否能解决问题:

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. 这将迫使您的WebView打开其内部的链接,因此,如果类似页面发出重定向,则cookie(如果有)不会丢失。

If problem still persists you can also try setting cookies manually by executing this before you load the url in your WebView: 如果问题仍然存在,您还可以尝试执行以下操作来手动设置cookie,然后再将其加载到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();

I think this is what you are looking for: https://developers.facebook.com/docs/authentication/server-side/ 我认为这就是您要寻找的: 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. 如果我没看错,那么实现OAuth后,就可以使用Graph API来实现所需的“赞”按钮,因为您可以对设备进行身份验证-而不仅仅是用户。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM