【问题标题】:Extracting Twitter PIN info, WP7提取 Twitter PIN 信息,WP7
【发布时间】:2012-07-03 07:07:02
【问题描述】:

我一直在关注这个很棒的教程:

http://buildmobile.com/twitter-in-a-windows-phone-7-app/#fbid=o0eLp-OipGa

但是,我使用的 pin 提取方法似乎对我不起作用或已过时。我不是 html 报废方面的专家,我想知道是否有人可以帮助我找到提取 pin 的解决方案。本教程使用的方法是:

private void BrowserNavigated(object sender, NavigationEventArgs e){
if (AuthenticationBrowser.Visibility == Visibility.Collapsed) {
    AuthenticationBrowser.Visibility = Visibility.Visible;
}
if (e.Uri.AbsoluteUri.ToLower().Replace("https://", "http://") == AuthorizeUrl) {
    var htmlString = AuthenticationBrowser.SaveToString();
    var pinFinder = new Regex(@"<DIV id=oauth_pin>(?<pin>[A-Za-z0-9_]+)</DIV>", RegexOptions.IgnoreCase);
    var match = pinFinder.Match(htmlString);
    if (match.Length > 0) {
        var group = match.Groups["pin"];
        if (group.Length > 0) {
            pin = group.Captures[0].Value;
            if (!string.IsNullOrEmpty(pin)) {
                RetrieveAccessToken();
            }
        }
    }
    if (string.IsNullOrEmpty(pin)){
        Dispatcher.BeginInvoke(() => MessageBox.Show("Authorization denied by user"));
    }
    // Make sure pin is reset to null
    pin = null;
    AuthenticationBrowser.Visibility = Visibility.Collapsed;
}

}

运行该代码时,“匹配”总是以 null 结尾,并且永远找不到 pin。本教程中的所有其他内容都有效,但由于页面的新结构,我不知道如何操作此代码来提取 pin。

我真的很感激时间,

迈克

【问题讨论】:

    标签: c# api windows-phone-7 twitter


    【解决方案1】:

    我发现 Twitter 有 2 个不同的 PIN 页面,我认为它们会根据您的浏览器确定将您重定向到哪个页面。

    像字符串解析这样简单的东西对你有用。我遇到的第一个 PIN 页面的 PIN 码包含在 <.code> 标记中,因此只需查找 <.code> 并解析它:

     if (innerHtml.Contains("<code>"))
     {
         pin = innerHtml.Substring(innerHtml.IndexOf("<code>") + 6, 7);
     }
    

    如果我没记错的话,我遇到的另一个页面(看起来像您正在使用的教程中的那个)是使用 id="oauth_pin" 包装的。所以,也只需解析它:

    else if(innerHtml.Contains("oauth_pin"))
    {
         pin = innerHtml.Substring(innerHtml.IndexOf("oauth_pin") + 10, 7);
    }
    

    innerHtml 是一个包含页面正文的字符串。这似乎是 var htmlString = AuthenticationBrowser.SaveToString();来自您的代码。

    我在我的 C# 程序中使用了这两种方法,它们工作得很好,完整的 sn-p:

    private void WebBrowser1DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
          var innerHtml = webBrowser1.Document.Body.InnerHtml.ToLower();
          var code = string.Empty;
          if (innerHtml.Contains("<code>"))
          {
              code = innerHtml.Substring(innerHtml.IndexOf("<code>") + 6, 7);
          }
          else if(innerHtml.Contains("oauth_pin"))
          {
              code = innerHtml.Substring(innerHtml.IndexOf("oauth_pin") + 10, 7);
          }
          textBox1.Text = code;
    }
    

    如果您有任何问题,请告诉我,希望对您有所帮助!

    【讨论】:

      【解决方案2】:

      我需要用这个更改 Toma A 建议的代码:

         var innerHtml = webBrowser1.SaveToString();
              var code = string.Empty;
      
              if (innerHtml.Contains("<code>"))
              {
                  code = innerHtml.Substring(innerHtml.IndexOf("<code>") + 6, 7);
      
              }
      
              else if (innerHtml.Contains("oauth_pin"))
              {
                  code = innerHtml.Substring(innerHtml.IndexOf("oauth_pin") + 10, 7);
              }
      

      因为这个不适用于windows phone

       var innerHtml = webBrowser1.Document.Body.InnerHtml.ToLower();
      

      【讨论】:

        猜你喜欢
        • 2014-10-07
        • 1970-01-01
        • 2015-05-25
        • 1970-01-01
        • 2011-06-03
        • 1970-01-01
        • 2023-03-21
        • 2020-02-05
        • 2015-02-25
        相关资源
        最近更新 更多