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 官方应用程序中,有上一个对话框,以及另一个用于选择要使用帐户的用户名的对话框:

除了第一个之外,我不知道如何从单个电子邮件帐户中获取个人用户名。仅使用 YouTube API 是否可行?

4

1 回答 1

1

我在 YouTube API 中找不到任何功能来执行此操作,因此我最终使用了 WebView 并使用了 YouTube 浏览器版本的帐户选择器。getAccessToken()我使用fromGoogleTokenResponse 作为身份验证的最终结果检索了访问令牌。

这是我用来执行此操作的主类,最初使用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 回答