1

我以为我已经成功实现了 YouTube 数据 API 来从我的应用程序上传视频,直到我不断收到com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized,对于我尝试遵循Tasks android sampleYouTube Java的实现来自网络的示例和其他示例。谁能发现导致问题的原因?

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"}
4

1 回答 1

1

I have fixed my problem

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();
于 2013-03-26T17:48:13.080 回答