2013-07-16 39 views
0

我試圖讓我的android應用程序的用戶登錄到他們的谷歌帳戶,所以我可以訪問他們的聯繫人爲創建一個朋友列表的目的,我試圖使用OAuth來做到這一點。 到目前爲止,我已經形成了URL並創建了加載頁面的webview。我繼續登錄,但我不知道如何將URL中的代碼分配給我的活動中的代碼值。我也不知道我的Onpagestarted方法是否正常工作如何解析web 2.0 Oauth 2.0代碼在android

有人可以告訴我使用OAuth2進行身份驗證的替代方法/技巧,或者告訴我使用OAuth2時有什麼問題,包含我的視圖代碼的頁面仍然是WebView,而不是包含我想要的代碼的textview?

我想在用戶登錄到我的WebView中的谷歌頁面後自動完成整個過程,但到目前爲止我的代碼一直被卡在Authcode的頁面,這導致我相信Authcode不是通過我的程序檢測到。

注:我不想簡單地使用被簽署成爲Android設備的帳戶(除非顯然用戶選擇輸入相同的帳戶)

這是從活動的代碼中,我想獲取身份驗證代碼/令牌並獲取我的用戶的聯繫人,但我不介意在以後的活動中顯示聯繫人。

WebView browser; 
final String username = "[email protected]"; 
private myWebViewClient client; 
String code; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_add_friends); 
    browser = (WebView) findViewById(R.id.webview); 
    client = new myWebViewClient(); 
    WebSettings webSettings = browser.getSettings(); 
    webSettings.setJavaScriptEnabled(true); 
    browser.setWebViewClient(client); 
    getAuthCode(client); 

} 

@Override 
public boolean onCreateOptionsMenu(Menu menu) { 
    // Inflate the menu; this adds items to the action bar if it is present. 
    getMenuInflater().inflate(R.menu.add_friends, menu); 
    return true; 

} 


public void getAuthCode(myWebViewClient myClient){ 
    browser.loadUrl(url4); 
    while (myClient.authCode!= null){ 
    showCode(myClient);} 
} 

public void showCode(myWebViewClient myClient){ 
     code = myClient.authCode; 
     TextView text = new TextView(this); 
     text.setText(code); 
     setContentView(text); 
} 

下面是我修改WebViewClient類

public class myWebViewClient extends WebViewClient{ 
boolean authComplete = false; 

String authCode = null; 

@Override public void onPageStarted(WebView view, String url, Bitmap favicon){ 
    super.onPageStarted(view, url, favicon); 
    if (url.contains("?code=") && authComplete != true) { 

     int codeStart = url.indexOf("?code=")+6; 


     authCode = url.substring(codeStart);  

     Log.i("", "AUTHCODE : " + authCode); 
     authComplete = true; 

     System.out.println(authCode); 

    }else if(url.contains("error=access_denied")){ 
     Log.i("", "ACCESS_DENIED_HERE"); 
     authComplete = true; 
    } 


    } 

} 

而且,如果我能得到這樣POST/GET在Java中的請求從谷歌收到JSON數據的一些一般性的指導。我對android開發非常陌生,並且我有接近0的處理html/http請求的經驗。

回答

0

看看this sample。這不是反對谷歌,但原則是相同的。您可能需要考慮在OAuth中使用「隱式」流程,以避免在設備中存儲祕密。