【发布时间】:2013-03-26 08:01:36
【问题描述】:
我以为我已经成功实现了 YouTube 数据 API 以从我的应用上传视频,直到我不断收到 com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized,对于实现我尝试关注Tasks android sample 和YouTube Java sample 以及来自网络的其他示例。任何人都可以找出导致问题的原因吗?
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"}
【问题讨论】:
-
"message": "Invalid Credentials" - 消息看起来非常清晰和明显。
-
我知道我似乎在浪费时间,但是我已经检查了很多东西,我想知道它们是否是我获取令牌的问题,使其无效,如果范围错误或任何其他任意细节。我一遍又一遍地检查并改变了事情,但我仍然得到相同的回应。我的代码似乎是标准的,有很多例子。
-
@RedChris .. 我无法弄清楚是什么让你的代码工作。你能帮我解决这个问题,因为我面临同样的问题。谢谢
-
我稍后会查看我的代码并尝试找到明确的答案,但这完全与凭据以及我如何创建它有关
标签: android youtube-api