2013-03-26 104 views
1

我以为我已成功实施YouTube数据API从我的应用上传视频直到我不断收到com.google.api.client.googleapis.json.GoogleJsonResponseException:401未授权,为了实现我尝试遵循Tasks android sampleYouTube 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"} 
+0

_ “消息”: “无效凭证” _ - 消息似乎非常清楚并且很明显。 – CBroe 2013-03-26 08:48:57

+0

我知道我好像在浪费时间,但是我检查了很多东西,我想知道他们是如何得到令牌的问题,使其无效,如果范围错误或任何其他任意详情。我一遍又一遍地检查,并改变了一切,但我仍然得到相同的回应。我的代码似乎是有很多示例的标准。 – RedChris 2013-03-26 09:27:17

+0

@RedChris ..我无法弄清楚什么让你的代码工作。你可以帮我解决这个问题,因为我面临同样的问题。谢谢 – Sushil 2014-12-01 17:21:30

回答

1

我已经固定我的问题

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(); 
+0

请您详细说明您的答案,以便我可以对其进行修改 – VVB 2015-02-13 07:31:34

+0

我做了你的答案中有相同的步骤,但这种方法已被弃用,不适用于我的工作。我的意思是,我得到了同样的结果。 – VVB 2015-02-13 10:01:16

+0

如果是这样的话,我不确定推荐什么。大约一年前我停止了这个项目。我唯一的想法是尝试找到一些完整的例子。 – RedChris 2015-02-13 10:09:10

相关问题