我可以使用youtube data api v3在youtube上获取用户的播放列表ID。此外,我可以使用youtube播放器api播放公开但不是私人播放列表的播放列表。
Youtube播放器api对我来说不是必须的,我可以切换到你可以提供的任何其他东西。我想要的只是播放私人播放列表。
虽然我的应用程序是由用户验证的(正如我所说,我已经可以访问用户的信息),但我收到一个错误:(来自logcat)
09-11 18:21:05.774: E/YouTubeAndroidPlayerAPI(21823): apps.youtube.datalib.common.b.n.a:36 Http error: status=[403] msg=[Forbidden, GData error(s):
09-11 18:21:05.774: E/YouTubeAndroidPlayerAPI(21823): [{domain:GData,code:ServiceForbiddenException,location:null,internalReason:User authentication required.}]]
我认为问题与youtube播放器api有关,它没有此功能,我必须以某种方式编辑youtube播放器api以支持播放私人播放列表。
这是我得到的错误的屏幕截图: http://s17.postimg.org/8vjvnd00f/Screenshot_2014_09_11_18_21_41.png
这些是我的问题相关的代码:(我有一个活动,这是主要的活动)
public static final String DEVELOPER_YTDATA_KEY="my key from google developer console";
public static final String TAG="bestsongs";
private Context context;
private final HttpTransport transport = AndroidHttp.newCompatibleTransport();
private final JsonFactory jsonFactory = new GsonFactory();
/**
* Define a global instance of a Youtube object, which will be used
* to make YouTube Data API requests.
*/
private static YouTube youtube;
private static final int REQUEST_ACCOUNT_PICKER = 2;
private static final int REQUEST_AUTHORIZATION = 3;
private String mChosenAccountName;
public static final String ACCOUNT_KEY = "accountName";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context=this;
chooseAccount();
}
public class GetPlaylist extends AsyncTask<Void, Void, String>{
/**
* Initialize a YouTube object to search for videos on YouTube. Then
* display the name and thumbnail image of each video in the result set.
*
* @param args command line args.
*/
@Override
protected String doInBackground(Void... arg0) {
String playlistID=null;
try {
String authToken = GoogleAuthUtil.getToken(context,
mChosenAccountName, "oauth2:" + YouTubeScopes.YOUTUBE);
GoogleCredential credential = new GoogleCredential();
credential.setAccessToken(authToken);
// This object is used to make YouTube Data API requests.
YouTube youtube = new YouTube.Builder(transport, jsonFactory,
credential).setApplicationName(Constants.APP_NAME)
.build();
Log.d(TAG, "a");
// Define the API request for retrieving search results.
PlaylistListResponse ytPlaylistResponse = youtube.playlists()
.list("id,snippet")
.setMine(true)
.execute();
//Iterate over results
List<Playlist> playlistResultList = ytPlaylistResponse.getItems();
if(playlistResultList!=null){
//for(int i=0; i<playlistResultList.size(); i++){
playlistID=playlistResultList.get(1).getId();
Log.d(TAG, "playlist id: "+playlistID);
//}
}
} catch (GoogleJsonResponseException e) {
Log.e(TAG,"There was a service error: " + e.getDetails().getCode() + " : "
+ e.getDetails().getMessage());
} catch (UserRecoverableAuthException e) {
// User needs to authorize this application, or take some other action
Intent authIntent = e.getIntent();
startActivityForResult(authIntent, REQUEST_AUTHORIZATION);
Log.e(TAG,"UserRecoverableAuthException: " + e.getCause() + " : " + e.getMessage() +" : "+ e.getLocalizedMessage());
} catch (GoogleAuthException e) {
Log.e(TAG,"GoogleAuthException: " + e.getCause() + " : " + e.getMessage() +" : "+ e.getLocalizedMessage());
} catch (IOException e) {
Log.e(TAG,"Playlist IOException: " + e.getCause() + " : " + e.getMessage() +" : "+ e.getLocalizedMessage());
}
return playlistID;
}
@Override
protected void onPostExecute(String playlistID){
if(YouTubeIntents.isYouTubeInstalled(context)) {
if(YouTubeApiServiceUtil.isYouTubeApiServiceAvailable(context)
== YouTubeInitializationResult.SUCCESS) {
// start the YouTube player
startActivity(
YouTubeStandalonePlayer.createPlaylistIntent(
(Activity) context, DEVELOPER_YTDATA_KEY, playlistID));
}
}
}
}
private void chooseAccount() {
Log.d(TAG, "in choose account");
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_AUTHORIZATION:
if (resultCode != Activity.RESULT_OK) {
chooseAccount();
Log.d(TAG, "in onresult request_auth");
}
break;
case REQUEST_ACCOUNT_PICKER:
if (resultCode == Activity.RESULT_OK && data != null
&& data.getExtras() != null) {
String accountName = data.getExtras().getString(
AccountManager.KEY_ACCOUNT_NAME);
if (accountName != null) {
mChosenAccountName = accountName;
Log.d(TAG, "accountName onresult: "+mChosenAccountName);
new GetPlaylist().execute();
}
}
break;
}
}
我已经尝试了GoogleAccountCredential.usingOAuth2身份验证,完全相同的问题。 (我的意思是这里的方法https://github.com/youtube/yt-direct-lite-android/blob/master/src/com/google/ytdl/MainActivity.java)