2013-10-11 59 views
3

在YouTube上的Java API我添加了一個登錄對話框,帳戶選擇這樣的:如何在YouTube API中獲取與Google帳戶關聯的多個用戶名?

enter image description here

這是我使用的代碼:

public void authenticate(){ 
    Intent accountChooserIntent = AccountPicker.newChooseAccountIntent(null, null, new String[] 
      {GoogleAuthUtil.GOOGLE_ACCOUNT_TYPE}, true, "Choose an account", null, null, null); 
    startActivityForResult(accountChooserIntent, AuthenticationConstants.REQUEST_PICK_ACCOUNT); 

}

在在官方的YouTube應用程序中有上一個對話框,以及另一個用於選擇使用該帳戶的用戶名:

我不知道如何從一個電子郵件帳戶,除了第一個之外獲得個人用戶名。這可能只使用YouTube API嗎?

+0

我也爲此而努力。你已經得到這個工作?如果你有。請告訴我你做了什麼。在此先感謝 –

回答

1

我在YouTube API中找不到任何功能來做到這一點,所以我最終使用了WebView並使用帳戶選擇器來瀏覽YouTube版本。作爲認證的最終結果,我使用GoogleTokenResponse中的getAccessToken()來檢索訪問令牌。

這裏是我用來做這個主類,最初被稱爲與AuthenticateWebView.loadWebView()

import android.os.AsyncTask;  
import android.webkit.WebView; 
import android.webkit.WebViewClient;  
import com.google.api.client.googleapis.auth.oauth2.GoogleTokenResponse; 
import com.google.api.client.json.JsonFactory; 
import com.google.api.client.googleapis.auth.oauth2.GoogleAuthorizationCodeTokenRequest; 
import com.google.api.client.http.HttpTransport; 
import com.google.api.client.http.javanet.NetHttpTransport; 
import com.google.api.client.json.jackson2.JacksonFactory; 

import java.io.IOException; 
import java.util.ArrayList; 

/** 
* Use a WebView to login in to Google Account and specific username within it. 
*/ 
public class AuthenticateWebView { 

    LoginActivity activity; 
    String authToken = ApplicationConstants.emptyString; 
    HttpTransport httpTransport = new NetHttpTransport(); 
    JsonFactory jacksonFactory = new JacksonFactory(); 
    WebView authWebView; 

    AuthenticateWebView(LoginActivity activity) { 
     this.activity= activity; 
    } 


    private void loadLayout(){ 
     activity.setContentView(R.layout.authentication_web_view); 
     authWebView = (WebView) activity.findViewById(R.id.authWebView); 
    } 

    protected void loadWebView(){ 
     loadLayout(); 
     authWebView.setWebViewClient(new WebViewClient(){ 
      @Override 
      public void onPageFinished(WebView view, String url){ 
       super.onPageFinished(view, url);    
       if (url.contains("https://accounts.google.com/o/oauth2/approval")){ 
        activity.setContentView(R.layout.activity_main);      
        searchForCodeOrError(authWebView.getTitle()); 
       } 
      } 
     }); 
     authWebView.getSettings().setJavaScriptEnabled(true); 
     authWebView.loadUrl("https://accounts.google.com/o/oauth2/auth?client_id="+  ApplicationConstants.CLIENT_ID+ 
          "&redirect_uri="+ ApplicationConstants.REDIRECT_URI+ 
          "&response_type="+ ApplicationConstants.RESPONSE_TYPE); 
    } 

    private void requestAccessToken(String authToken){ 
     final String requestToken = authToken; 
     try{ 
      new AsyncTask<String, Void, GoogleTokenResponse>(){ 
        GoogleTokenResponse response = null; 
        @Override 
        public GoogleTokenResponse doInBackground(String... params){ 
         try { 
          response = new GoogleAuthorizationCodeTokenRequest(httpTransport, jacksonFactory, ApplicationConstants.CLIENT_ID 
           , ApplicationConstants.CLIENT_SECRET, params[0], ApplicationConstants.REDIRECT_URI).execute(); 
         } catch (IOException e1) { 
          //catches TokenResponseException        
          requestAccessToken(requestToken); 
         } 
         return response; 
        } 
        @Override 
        public void onPostExecute(GoogleTokenResponse response1){ 
         activity.currentAccessToken = response1.getAccessToken(); 
         if (activity.currentAccessToken != null){ 
          activity.mToken = activity.currentAccessToken; 
          activity.loadListHeaderandFooter(); 
          activity.loadData(); 
         } 
        } 
      }.execute(authToken); 
     }catch (Exception e){ 
       Log.e("Error occured in AuthenticateWebview.requestAccessToken()", e.getMessage()); 
     } 
    } 


    private void searchForCodeOrError(String pageTitle){   
     if (pageTitle.contains("code=") | pageTitle.contains("error=")){ 
      ArrayList<Character> charList = new ArrayList<Character>();    
      for (char c: pageTitle.toCharArray()){ 
       charList.add(c); 
       if (charList.contains("code=") | charList.contains("error=")){ 
        charList.clear(); 
       } 
      } 

      for (char c: charList){ 
       authToken += c; 
      } 
      authToken = authToken.substring(13);   
      requestAccessToken(authToken); 

     } 
    } 
} 
+0

這太棒了! :) 謝謝! – 8m47x

相關問題