我以为自己已经成功实现了YouTube数据API,可以从我的应用中上传视频,直到不断获取com.google.api.client.googleapis.json.GoogleJsonResponseException:401未经授权,对于该实现,我尝试遵循Tasks android 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", "[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"}

最佳答案

我已经解决了我的问题

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

10-08 15:08