2011-11-16 67 views
5

目前,我们正在研究使用Phonegap的应用程序的最后一步,并且已经触及黑莓端口的一些问题。通过oAUTH使用Phonegap for Blackberry进行身份验证

到目前为止,我们一直在审查网上提供的内容,无法找到真正压轴的答案。似乎Twitter,Facebook或Foursquare的“正确”方法使oauth认证过程可以使用ChildBrowser插件,实例化一个窗口,然后用它来处理该过程。

说得对,黑莓似乎缺少一个ChildBrowser插件。我们一直在寻找Github上的几个私人项目,看起来他们构建/使用该功能,但我们不确定如何控制创建的窗口。

这些插件的大部分(或全部?)是指调用原生Blackberry浏览器来处理URLS,但是如何设法处理回调,获取令牌并关闭窗口,因为这是另一个过程。

例如,我们有这个概念代码:

function openWindow() { 
    if (typeof blackberry !== 'undefined') { 
    app_id = SOMETHING_HERE; 
    redirect = 'http://www.facebook.com/connect/login_success.html'; 
    url = 'https://graph.facebook.com/oauth/authorizeclient_id='+app_id+'&redirect_uri='+redirect+'&display=touch&scope=publish_stream'; 
    var args = new blackberry.invoke.BrowserArguments(url); 
    blackberry.invoke.invoke(blackberry.invoke.APP_BROWSER, args); 
      } 
     } 

这对于打开URL工作,但仅此而已。有没有办法在窗口上获得句柄并为事件注入一些监听器?什么应该是我们的正确方法?

谢谢!

回答

3

我不是PhoneGap的用户,但我们却必须处理非常类似的情景 - 本机应用程序调用手机浏览器提示的OAuth流,然后能够处理回调到aative应用。

这有可能在使用BrowserContentProviderRegistry API黑莓。只要将特定的MIME类型返回给浏览器,就可以注册您的应用程序。听起来很复杂,但在所有作品中都相当简单。

这里是粗糙的流量 -

  1. 本机应用程序调用浏览器对网页的OAuth。这部分很容易,看起来像你有这个部分。
  2. 的OAuth的重定向需要去,你可以控制的URL。类似于http://mycompany.com/oAuthRedirectHandler.asp
  3. 的oAuthRedirectorHandler.asp简单像这样的代码(我们选择了传统的ASP,但这可以用PHP来完成或任何语言,你也可以忽略以下的Android块) -

    <html><body> 
    <h1>Redirect page</h1> 
    If you are not re-directed, please open the application manually. 
    <% strUA = Request.ServerVariables("HTTP_USER_AGENT") 
    if (InStr(strUA, "BlackBerry")) then  
         Response.Write("Opening appplication on BlackBerry") 
         Response.ContentType="application/x-MyCustomApp" 
    elseif (InStr(strUA, "Android")) then 
         Response.Write("Opening appplication on Android")  
         Response.Redirect("MyCustomApp://mycompany.com") 
    end if %> 
    </body> </html> 
    
  4. 在黑莓手机代码你想要一个新BrowserContentProvider这样的 -

    final class CustomBrowserProvider extends BrowserContentProvider{ 
        String[] ACCEPT = new String[]{"application/x-MyCustomApp}; 
        String appName; 
    
        CustomBrowserProvider(String appName){ 
        this.appName = ApplicationDescriptor.currentApplicationDescriptor().getModuleName(); 
        //cache this appName from the constructor in the invocation code below. 
        } 
    
        public String[] getSupportedMimeTypes() { return ACCEPT;} 
        public String[] getAccept(RenderingOptions context){return ACCEPT;} 
    
        public BrowserContent getBrowserContent(BrowserContentProviderContext context) throws RenderingException { 
        //this is where the callback happens 
        //this is happening in a separate process, raise your main app here using the appName that got passed in 
        //I dont have a sanitized ready to go sample to post here on how to do this, but not too complicated 
        //as a hint use the ApplicationDescriptor and CodeModuleManager classes 
        return null; 
        } 
    } 
    
  5. 现在,在您的应用程序初始化,注册这个新BrowserPlugin这样的 -

    BrowserContentProviderRegistry converterRegistry = BrowserContentProviderRegistry.getInstance(); 
    converterRegistry.register(new CustomBrowserProvider());    
    

希望这会有所帮助。这工作很适合我们。一个缺点,我们在这里度过的是,当用户返回到浏览应用程序,他们留下的空白页,并没有关闭,在BB没有什么好办法。

+0

哇,这看起来很有趣。非常感谢您的意见!将尝试并报告我发现的内容。 – Yaraher

相关问题