【问题标题】:Authenticating via oAUTH with providers using Phonegap for Blackberry通过 oAUTH 与使用 Phonegap for Blackberry 的提供商进行身份验证
【发布时间】:2011-11-16 16:28:04
【问题描述】:

我们目前正在对一个使用 Phonegap 的应用程序进行最后润色,并遇到了一些与 Blackberry 端口有关的问题。

到目前为止,我们一直在审查在线可用的内容,但找不到真正的最终答案。似乎为 Twitter、Facebook 或 Foursquare 制作和 oauth 身份验证过程的“正确”方法是使用 ChildBrowser 插件,实例化一个窗口,然后使用它来处理该过程。

没错,Blackberry 似乎缺少 ChildBrowser 插件。到目前为止,我们一直在查看 Github 上的几个私人项目,它们看起来像是在构建/使用该功能,但我们不确定如何控制创建的窗口。

这些插件中的大多数(或全部?)指的是调用本机黑莓浏览器来处理 URL,但是由于它是另一个进程,因此如何管理回调、获取令牌并关闭窗口。

比如我们有这个概念代码:

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,仅此而已。有没有办法获得窗口句柄并为事件注入一些侦听器?我们的正确做法应该是什么?

谢谢!

【问题讨论】:

    标签: blackberry oauth cordova


    【解决方案1】:

    我不是 PhoneGap 用户,但我们确实必须处理一个非常相似的场景 - 原生应用程序调用移动浏览器来提示 oAuth 流程,然后能够处理对 aative 应用程序的回调。

    这可以在 BlackBerry 上使用 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 中关闭该页面。

    【讨论】:

    • 哇,这看起来很有趣。非常感谢您的意见!将尝试报告我的发现。
    猜你喜欢
    • 2019-08-11
    • 2018-08-26
    • 2020-09-17
    • 1970-01-01
    • 2015-01-25
    • 1970-01-01
    • 2014-09-11
    • 1970-01-01
    • 2013-12-05
    相关资源
    最近更新 更多