1

com.google.api.client.googleapis.json.GoogleJsonResponseException: 401 Unauthorized取得し続けるまで、アプリから動画をアップロードするためのYouTubeデータ API の実装に成功したと思いました。 Web からのサンプルとその他の例。誰でも問題の原因を特定できますか?

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

問題を解決しました

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 に答える