3

YouTube Java API で、次のようなアカウント チューザーを含むログイン ダイアログを追加しました。

ここに画像の説明を入力

これは私が使用したコードです:

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 アプリには、前のダイアログと、アカウントで使用するユーザー名を選択する別のダイアログがあります。

最初のメール アカウント以外に、1 つのメール アカウントから個々のユーザー名を取得する方法がわかりません。これは YouTube API のみを使用して可能ですか?

4

1 に答える 1

1

これを行うための関数が YouTube API に見つからなかったので、最終的に WebView を使用し、YouTube のブラウザー バージョンのアカウント チューザーを使用しました。 認証の最終結果としてを使用getAccessToken()してアクセス トークンを取得しました。GoogleTokenResponse

これは、私がこれを行うために使用したメインクラスであり、最初に呼び出されました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);

        }
    }
}
于 2014-01-28T02:22:47.500 に答える