我以为我已成功实施YouTube数据API从我的应用上传视频直到我不断收到com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权,为了实现我尝试遵循Tasks android sample和YouTube Java sample以及其他来自Web的示例。任何人都可以发现导致问题的原因吗?401未经授权使用YouTube数据API V3
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", "[email protected]"));
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"}
_ “消息”: “无效凭证” _ - 消息似乎非常清楚并且很明显。 – CBroe 2013-03-26 08:48:57
我知道我好像在浪费时间,但是我检查了很多东西,我想知道他们是如何得到令牌的问题,使其无效,如果范围错误或任何其他任意详情。我一遍又一遍地检查,并改变了一切,但我仍然得到相同的回应。我的代码似乎是有很多示例的标准。 – RedChris 2013-03-26 09:27:17
@RedChris ..我无法弄清楚什么让你的代码工作。你可以帮我解决这个问题,因为我面临同样的问题。谢谢 – Sushil 2014-12-01 17:21:30