401使用YouTube Data API V3时未经授权

时间:2013-03-26 08:01:36

标签: android youtube-api

我认为我已经成功实施了YouTube数据API,以便从我的应用上传视频,直到我不断收到 com.google.api.client.googleapis.json.GoogleJsonResponseException:401 Unauthorized ,对于实施我尝试了网络上的Tasks android sampleYouTube 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"}

1 个答案:

答案 0 :(得分: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();