bobygerm
bobygerm

Reputation: 483

Unexpected response code (401) when requesting: writeMoment

My android app is using the Google+ API to connect the user to this social network and to record activities in it.

My code is straightforward, I copied the sample code to write a review activity as a moment here : https://developers.google.com/+/mobile/android/app-activities

monPlusClient = new PlusClient.Builder(this, 
    new GooglePlayServicesClient.ConnectionCallbacks() {

        @Override
        public void onDisconnected() {
            Log.d(TAG_DEBUG, "monPlusClient, onDisconnected");
        }

        @Override
        public void onConnected(Bundle connectionHint) {
            String accountName = monPlusClient.getAccountName();
            Log.d(TAG_DEBUG, "monPlusClient, onConnected : "    + accountName);
            String targetUrl = "https://developers.google.com/+/mobile/android/getting-started";
            ItemScope rating = new ItemScope.Builder()
                    .setType("http://schema.org/Rating")
                    .setRatingValue("100").setBestRating("100")
                    .setWorstRating("0").build();
            ItemScope result = new ItemScope.Builder()
                    .setType("http://schema.org/Review")
                    .setName("A Humble Review of Widget")
                    .setUrl("https://developers.google.com/+/web/snippet/examples/review")
                    .setDescription("It is amazingly effective")
                    .setReviewRating(rating).build();
            ItemScope target = new ItemScope.Builder().setUrl(targetUrl).build();
            Moment moment = new Moment.Builder()
                        .setType("http://schemas.google.com/ReviewActivity")
                        .setTarget(target).setResult(result)
                        .build();
            monPlusClient.writeMoment(moment);
            Log.d(TAG_DEBUG, "ActiviteListeVoyages, monPlusClient, writeMoment effectué");
            Toast.makeText( ActiviteListeVoyages.this,
                        ActiviteListeVoyages.this.getResources().getString(R.string.textesauvegarde),
                        Toast.LENGTH_LONG).show();
            }, 
    new GooglePlayServicesClient.OnConnectionFailedListener() {

        @Override
        public void onConnectionFailed(ConnectionResult resultat) {
            Log.d(TAG_DEBUG, "monPlusClient, onConnectionFailed");
            if (resultat.hasResolution()) {
                try {                           
                    resultat.startResolutionForResult(
                                    ActiviteListeVoyages.this,
                                    REQUEST_CODE_RESOLVE_ERR);
                } catch (SendIntentException e) {

                }
            }
            // Save the result and resolve the connection failure
            // upon a user click.
            ResultatConnexion = resultat;
        }

    }).setScopes(Scopes.PLUS_LOGIN)
    .setVisibleActivities("http://schemas.google.com/AddActivity",
                    "http://schemas.google.com/DiscoverActivity",
                    "http://schemas.google.com/ReviewActivity").build();

When I execute the app, I can see that the connection is OK but when witing a moment, the logcat displays :

08-31 17:04:08.410: W/ejk(528): Authentication error: Unable to respond to any of these challenges: {bearer=WWW-Authenticate: Bearer realm="https://www.google.com/accounts/AuthSubRequest", error=invalid_token}
08-31 17:04:08.410: I/qtaguid(528): Failed write_ctrl(u 64) res=-1 errno=22
08-31 17:04:08.410: I/qtaguid(528): Untagging socket 64 failed errno=-22
08-31 17:04:08.440: W/NetworkManagementSocketTagger(528): untagSocket(64) failed with errno -22
08-31 17:04:08.450: E/Volley(528): [71] je.a: Unexpected response code 401 for https://www.googleapis.com/plus/v1/people/me/moments/vault
08-31 17:04:08.450: D/GooglePlusPlatform(528): Unexpected response code (401) when requesting: writeMoment
08-31 17:04:08.470: I/GooglePlusPlatform(528): {"code":401,"errors":[{"message":"Unauthorized","domain":"global","reason":"unauthorized"}]}
08-31 17:04:08.480: D/dalvikvm(528): GC_CONCURRENT freed 634K, 12% free 8315K/9415K, paused 25ms+5ms, total 73ms
08-31 17:04:08.520: D/SyncManager(272): failed sync operation [email protected] (com.google), com.google.android.gms.plus.action, USER, earliestRunTime 78281280, SyncResult: stats [ numIoExceptions: 1]
08-31 17:04:15.489: D/Finsky(5255): [1] 5.onFinished: Installation state replication succeeded.

I registered my app in the Google API console. I have also another app that is able to write mements in Google+ and the two manifest are similar.

What did I forget ?

After revoking several times the G+ token and recreating the client ID in API console, the error has a code 500 error now :

09-03 22:43:41.450: D/GooglePlusPlatform(1146): Unexpected response code (500) when requesting: writeMoment
09-03 22:43:41.460: I/GooglePlusPlatform(1146): {"code":500}
09-03 22:43:43.460: D/SyncManager(272): failed sync operation [email protected] (com.google), com.google.android.gms.plus.action, USER, earliestRunTime 357855202, SyncResult: stats [ numIoExceptions: 1]

Here are the device screens when the user authenticates : enter image description here enter image description here

Upvotes: 0

Views: 4427

Answers (1)

Prisoner
Prisoner

Reputation: 50711

The relevant line in the error is probably

08-31 17:04:08.470: I/GooglePlusPlatform(528): {"code":401,"errors":[{"message":"Unauthorized","domain":"global","reason":"unauthorized"}]}

Indicating that there is an authorization error. Since it looks like you are asking for the valid scope, as well as the visible activity you need, the problem probably lies elsewhere. Did you enable the Google+ API in the code console? Have you successfully authenticated as this user and authorized actions under this scope?

Upvotes: 2

Related Questions