2012-10-11 38 views
0

我在創建可訪問Google帳戶的應用程序時遇到問題。我有以下代碼獲取登錄谷歌帳戶Signin Page頁面,然後訪問權限頁面也顯示! enter image description here。一旦我點擊「允許訪問」該應用程序被重定向到錯誤頁面Error。這裏有一些截圖,這樣不管是誰試圖幫助可以更好地理解..以下是我使用WP7上的Google Access

private void browseGoogle_Loaded(object sender, RoutedEventArgs e) 
    { 
     string address = "https://accounts.google.com/o/oauth2/auth" + 
     "?client_id=" + "*******.apps.googleusercontent.com" + 
     "&scope=" + "https://www.googleapis.com/auth/plus.me" + 
     "&response_type=code" + 
     "&redirect_uri=" + "https://www.****.com/oauth2callback"; 

     browseGoogle.Navigate(new Uri(address, UriKind.Absolute)); 

    } 

https://accounts.google.com/o/oauth2/approval?as=634f855389bf10ff&hl=en_GB&xsrfsign=APsBz4gAAAAAUHZvwB3xTqisyv8hEcWem5X3eKvwAHN9 的代碼,這是URI的導航,讓後選擇進入/點擊。這是什麼意思?

這就是我要做的。截至目前,我的BrowserNavigated方法不包含任何代碼。我不知道該怎麼做,希望得到幫助。 請幫我解決這個問題..所有的答案和建議表示讚賞。

+0

是否將url設置爲redirect_uri存在? 如果直接導航到該網址,會發生什麼情況? –

+0

該網址是虛擬的..這是我用於我的Twitter訪問應用程序也是一樣的URL ...你認爲這就是錯誤?那麼應該在這種情況下給出重定向URL呢? – Apoorva

+1

或將redirect_uri參數設置爲「urn:ietf:wg:oauth:2.0:oob」,它將導航到谷歌的默認重定向頁面。 –

回答

2
private void browseGoogle_Loaded(object sender, RoutedEventArgs e) 
    { 
     try 
     { 
      StringBuilder autheticateURL = new StringBuilder(); 
      autheticateURL.Append(GmailSettings.AuthorizeUri).Append("?client_id=").Append(GmailSettings.clientID).Append("&scope="). 
       Append(GmailSettings.ScopeValue).Append("&response_type=code").Append("&redirect_uri=").Append(GmailSettings.CallbackUri); 
      browseGoogle.Navigate(new Uri(autheticateURL.ToString(), UriKind.Absolute)); 
     } 
     catch (Exception ex) 
     { 

      Logger.log(TAG, "browseGoogle_Loaded()", ex.Message); 

     } 
    } 

    /// <summary> 
    /// Called when the web browser initiates Navigation to various pages 
    /// </summary> 
    /// <param name="sender">Browser</param> 
    /// <param name="e">Navigating event arguments</param> 
    private void browseGoogle_Navigating(object sender, NavigatingEventArgs e) 
    { 
     try 
     { 
      string hostName = e.Uri.Host; 
      string URL = e.Uri.ToString(); 

      if (hostName.StartsWith("localhost")) 
      { 
       NavigationService.Navigate(new Uri("/HomePage.xaml", UriKind.Relative)); 
      } 
     } 
     catch (Exception ex) 
     { 

      Logger.log(TAG, "browseGoogle_Navigating()", ex.Message); 

     } 
    } 

的XAML是這樣

<phone:WebBrowser x:Name="browseGoogle" Loaded="browseGoogle_Loaded" IsScriptEnabled="true" Navigating="browseGoogle_Navigating" /> 

我的錯誤有兩個: - 1)維涅什在他的評論有提到我使用的是錯誤的重定向URI。 2)IsScriptEnabled在我的Web瀏覽器控件中完全沒有設置。一旦我確定它是真的,一切都很好。

2

只需檢查GDrive開源應用程序代碼here

授權ViewViewModel向您展示如何使用Google憑據進行OAuth登錄。

下載代碼,閱讀網站上的預生成說明,並進行測試!

+0

它使用與上面相同的代碼進行非常小的更改。它開始工作,只要我在我已經錯過了的Web瀏覽器控件中添加IsScriptEnabled =「True」..感謝您的建議:) – Apoorva

+1

太棒了,很高興問題解決了! :) –