【问题标题】:How do I get multiple usernames associated with a Google account in the YouTube API?如何在 YouTube API 中获取与 Google 帐户关联的多个用户名?
【发布时间】:2013-10-19 00:46:02
【问题描述】:

在 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 是否可行?

【问题讨论】:

  • 我也在为此苦苦挣扎。你已经开始工作了吗?如果你这样做了。请告诉我你做了什么。提前致谢

标签: java android api youtube


【解决方案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);

        }
    }
}

【讨论】:

  • 太棒了! :) 谢谢!
猜你喜欢
  • 2012-11-27
  • 1970-01-01
  • 1970-01-01
  • 2016-02-19
  • 2020-02-04
  • 1970-01-01
  • 1970-01-01
  • 2013-03-09
  • 1970-01-01
相关资源
最近更新 更多