在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吗?
答案 0 :(得分: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);
}
}
}