我想创建一个应用程序,让用户登录他的Youtube帐户,从他的帐户中选择一个播放列表,然后播放视频。
我正在尝试使用此文档连接到Youtube:
这是我在MainActivity中的相关代码(只是试图从YT API获得任何反应):
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// ...
btnLogin = (Button)findViewById(R.id.btnlogin);
btnLogin.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View arg0) {
Intent intent = AccountPicker.newChooseAccountIntent(null, null, new String[]{"com.google"},
false, null, null, null, null);
startActivityForResult(intent, REQUEST_ACCOUNT_PICKER);
}});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
// ...
case REQUEST_ACCOUNT_PICKER:
if (resultCode == Activity.RESULT_OK && data != null
&& data.getExtras() != null) {
Log.i("VWL", "Callback");
String accountName = data.getExtras().getString(
AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
credential =
GoogleAccountCredential.usingOAuth2(this, Collections.singleton(YouTubeScopes.YOUTUBE_READONLY));
credential.setSelectedAccountName(accountName);
YouTube y = new YouTube.Builder(new NetHttpTransport(), new JacksonFactory(), credential).setApplicationName("VideoWhiteList").build();
try {
Log.i("VWL", "Playlist: " + y.playlists().list("snippet").setMine(true).execute().size());
} catch (IOException e) {
e.printStackTrace();
}
Log.i("VWL", "Account picked!");
} else {
Log.i("VWL", "Something's fishy!");
}
}
break;
}
}
清单:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="x.y.z">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS"/>
<uses-permission android:name="android.permission.MANAGE_ACCOUNTS"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name="x.y.z.MainActivity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation|screenSize">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
出现AccountPicker,我可以选择我的帐户。之后,尝试通过y.playlists().
行
02-24 11:19:27.535 3379-3379/? E/ExceptionHandler: java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=2, result=-1, data=Intent { (has extras) }} to activity {x.y.z/x.y.z.MainActivity}: java.lang.IllegalArgumentException: the name must not be empty: null
任何提示?