我以为我已经成功地实现了YouTube数据API从我的应用程序上传视频,直到我不断得到com.google.api.client.googleapis.json。GoogleJsonResponseException: 401 Unauthorized,为了实现我尝试了以下任务android样本和YouTube Java样本和其他来自web的例子。有人能找出问题的根源吗?
public class splash{
AccountManager.get(getApplicationContext()).getAuthTokenByFeatures(
"com.google", "oauth2:https://gdata.youtube.com", null,
this, null, null, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
String acc_name = bundle
.getString(AccountManager.KEY_ACCOUNT_NAME);
String auth_token = bundle
.getString(AccountManager.KEY_AUTHTOKEN);
Log.d("", "name: " + acc_name + "; token: "
+ auth_token);
editor.putString("USER_ACCOUNT_PREF", acc_name);
editor.putString("USER_ACCOUNT_AUTH_PREF", auth_token);
} catch (Exception e) {
Log.e("", e.getClass().getSimpleName() + ": "
+ e.getMessage());
}
}
}, null);
从public class myuploadclass {
private static YouTube youtube;
GoogleAccountCredential credential;
GoogleCredential CREDENTIAL = new GoogleCredential();
com.google.api.services.youtube.YouTube service;
final HttpTransport transport = AndroidHttp.newCompatibleTransport();
final JsonFactory jsonFactory = new GsonFactory();
final String CLIENT_ID = "blah.apps.googleusercontent.com";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
String test = mPreferences.getString("USER_ACCOUNT_AUTH_PREF", null);
AccountManager.get(getApplicationContext()).invalidateAuthToken("com.google", test);
final SharedPreferences.Editor editor = mPreferences.edit();
AccountManager.get(getApplicationContext()).getAuthTokenByFeatures(
"com.google", "oauth2:https://gdata.youtube.com", null,
this, null, null, new AccountManagerCallback<Bundle>() {
@Override
public void run(AccountManagerFuture<Bundle> future) {
try {
Bundle bundle = future.getResult();
String acc_name = bundle
.getString(AccountManager.KEY_ACCOUNT_NAME);
String auth_token = bundle
.getString(AccountManager.KEY_AUTHTOKEN);
Log.d("", "name: " + acc_name + "; token: "
+ auth_token);
editor.putString("USER_ACCOUNT_PREF", acc_name);
editor.putString("USER_ACCOUNT_AUTH_PREF", auth_token);
} catch (Exception e) {
Log.e("", e.getClass().getSimpleName() + ": "
+ e.getMessage());
}
}
}, null);
editor.commit();
String test2 = mPreferences.getString(
"USER_ACCOUNT_AUTH_PREF", null);
Log.i(mPreferences.getString(
"USER_ACCOUNT_PREF", null),mPreferences.getString(
"USER_ACCOUNT_AUTH_PREF", null));
credential = GoogleAccountCredential.usingOAuth2(this,
YouTubeScopes.YOUTUBE);
credential.setSelectedAccountName(mPreferences.getString(
"USER_ACCOUNT_PREF", "chrisdunne92@gmail.com"));
CREDENTIAL.setAccessToken(test2);
youtube = new YouTube.Builder(
transport, jsonFactory, credential).setApplicationName(
"application name").setHttpRequestInitializer(CREDENTIAL)
.setGoogleClientRequestInitializer(new YouTubeRequestInitializer(CLIENT_ID))
.build();
Logcat输出
03-26 07:40:15.890: W/System.err(13965): com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized
{"code": 401,"errors": [{"domain": "global","location": "Authorization","locationType": "header","message": "Invalid Credentials","reason": "authError" }],"message": "Invalid Credentials"}
我已经解决了我的问题
credential = GoogleAccountCredential.usingOAuth2(this,
YouTubeScopes.YOUTUBE_UPLOAD,YouTubeScopes.YOUTUBEPARTNER,YouTubeScopes.YOUTUBE);
credential.setSelectedAccountName(mPreferences.getString(PREF_ACCOUNT_NAME, null));
youtube = new YouTube.Builder(
transport, jsonFactory, credential).setApplicationName(
"PoliceWitness").build();