mongodb / stitch-android-sdk

MongoDB Stitch Android SDK
Apache License 2.0
58 stars 33 forks source link

(UNKNOWN): Unknown: com.mongodb.stitch.core.StitchServiceException: expected either accessToken or authCode in payload #174

Closed h-amg closed 5 years ago

h-amg commented 5 years ago

This error occurs when attempting to log in with a google account. i have enabled google sign in from the provider tab on stitch web console and followed all the instruction provided in your documentation. however i'm not sure what to user for client secret since google ow uses Signing-certificate fingerprint and app package name. i tried filling the Client Secret field with random text just to test it and see if it works regardless but as expected it didn't work.

The full error message in android studio:

(UNKNOWN): Unknown: com.mongodb.stitch.core.StitchServiceException: expected either accessToken or authCode in payload at com.mongodb.stitch.core.internal.common.StitchError.handleRequestError(StitchError.java:62) at com.mongodb.stitch.core.internal.net.BaseStitchRequestClient.inspectResponse(BaseStitchRequestClient.java:54) at com.mongodb.stitch.core.internal.net.BaseStitchRequestClient.doRequestUrl(BaseStitchRequestClient.java:65) at com.mongodb.stitch.core.internal.net.StitchAppRequestClientImpl.doRequest(StitchAppRequestClientImpl.java:55) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.doLoginRequest(CoreStitchAuth.java:663) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.doLogin(CoreStitchAuth.java:632) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.loginWithCredentialInternal(CoreStitchAuth.java:382) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl.access$000(StitchAuthImpl.java:48) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl$1.call(StitchAuthImpl.java:105) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl$1.call(StitchAuthImpl.java:102) at com.mongodb.stitch.core.internal.common.ThreadDispatcher$1.run(ThreadDispatcher.java:57) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:423) at java.util.concurrent.FutureTask.run(FutureTask.java:237) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588) at java.lang.Thread.run(Thread.java:818)

jsflax commented 5 years ago

Hi @h-amg ,

Could you please paste us some sample code? Whatever the latest Google Sign In SDK is should handle most of the lifting for you.

h-amg commented 5 years ago

Hi @jsflax

Sample code:

public class LoginActivity extends AppCompatActivity {
    private static final String TAG = "LoginActivity";

    @BindView(R2.id.google_signin)
    Button _google_signin;

    final StitchAppClient client = Stitch.getDefaultAppClient();
    private GoogleSignInClient googleSignInClient;
    final int RC_SIGN_IN = 7;  // activity request code

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);

        //initiate google sign-in client
        GoogleSignInOptions gso = new GoogleSignInOptions.Builder(
                GoogleSignInOptions.DEFAULT_SIGN_IN).requestEmail().build();
        googleSignInClient = GoogleSignIn.getClient(this, gso);

        // google sign-in button onclick listbner
        _google_signin.setOnClickListener(this::googleLogIn);

    }

    /** Google login **/
    public void googleLogIn(final View ignored) {

        Intent signInIntent = googleSignInClient.getSignInIntent();
        startActivityForResult(signInIntent, RC_SIGN_IN);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (requestCode == RC_SIGN_IN) {
            Task<GoogleSignInAccount> task = GoogleSignIn.getSignedInAccountFromIntent(data);
            handleGooglSignInResult(task);
            return;
        }
    }

    private void handleGooglSignInResult(Task<GoogleSignInAccount> completedTask) {
        try {
            GoogleSignInAccount account = completedTask.getResult(ApiException.class);
            final GoogleCredential googleCredential =
                    new GoogleCredential(account.getServerAuthCode());

            client.getAuth().loginWithCredential(googleCredential).addOnCompleteListener(
                    new OnCompleteListener<StitchUser>() {
                        @Override
                        public void onComplete(@NonNull final Task<StitchUser> task) {
                            if (task.isSuccessful()) {
                                // Do something here if the user logged in successfully.
                                Log.d("stitch", "Successfully logged in as user "
                                        + task.getResult().getId());

                            } else {
                                Log.e(TAG, "Error logging in with Google", task.getException());
                                Toast.makeText(LoginActivity.this,
                                        task.getException().getMessage(), Toast.LENGTH_SHORT).show();
                                onLoginFailed();
                            }
                        }
                    });
        } catch (ApiException e) {
            Log.w(TAG, "signInResult:failed code=" + e.getStatusCode());
        }
    }
}
h-amg commented 5 years ago

@jsflax , were you able to replicate the error?

jpbulbulian commented 5 years ago

I have a similar exception from one day to another...

I'm gettting a valid serverAuthCode from Google, but the loginWithCredential method from the Stitch Android SDK can't see it.

(UNKNOWN): Unknown: com.mongodb.stitch.core.StitchServiceException: expected payload to have one of: accessToken, id_token or authCode at com.mongodb.stitch.core.internal.common.StitchError.handleRichError(StitchError.java:94) at com.mongodb.stitch.core.internal.common.StitchError.handleRequestError(StitchError.java:61) at com.mongodb.stitch.core.internal.net.BaseStitchRequestClient.inspectResponse(BaseStitchRequestClient.java:54) at com.mongodb.stitch.core.internal.net.BaseStitchRequestClient.doRequestUrl(BaseStitchRequestClient.java:65) at com.mongodb.stitch.core.internal.net.StitchAppRequestClientImpl.doRequest(StitchAppRequestClientImpl.java:55) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.doLoginRequest(CoreStitchAuth.java:663) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.doLogin(CoreStitchAuth.java:632) at com.mongodb.stitch.core.auth.internal.CoreStitchAuth.loginWithCredentialInternal(CoreStitchAuth.java:382) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl.access$000(StitchAuthImpl.java:48) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl$1.call(StitchAuthImpl.java:105) at com.mongodb.stitch.android.core.auth.internal.StitchAuthImpl$1.call(StitchAuthImpl.java:102) at com.mongodb.stitch.core.internal.common.ThreadDispatcher$1.run(ThreadDispatcher.java:57) at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:458) at java.util.concurrent.FutureTask.run(FutureTask.java:266) at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1167) at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:641) at java.lang.Thread.run(Thread.java:764)

jpbulbulian commented 5 years ago

@h-amg for the Client Secret you have to create an Oauth ID for Web. Then use it to get the authCode. I 'm having an issue with the next step. Either Google and Facebook login it's not working.

h-amg commented 5 years ago

Hi @jpbulbulian while google login didn't work for me at all, Facebook log in returns task.isSuccessful() == False, while actually authenticating the user successfully, so if you send the user to the main activity regardless and checked for their authentication there, it will pass.

jpbulbulian commented 5 years ago

Hi @h-amg, the same it's happening to me. The login stage between mi client and Google/Facebook works fine, but when I send the AuthCode to Stitch I'm getting that exception. The problem started a few weeks ago, It's seems than something changes between Stitch and Google/Facebook.

argz777 commented 5 years ago

For me, I'm testing facebook login via stitch. On facebook side, it works properly, but when I pass the loginResult.getAccessToken().getToken(), I get "(UNKNOWN): Unknown: com.mongodb.stitch.core.StitchServiceException: expected payload to have one of: accessToken, id_token or authCode".

german-wings commented 5 years ago

Any updates on this ?

h-amg commented 5 years ago

nope

jsflax commented 5 years ago

@h-amg I created a google sign in application today myself. It provided me with the fields required to fill in the provider Stitch-side. Their instructions aren't great– be sure to add the Web credentials to the Stitch UI, and the Android ones set up Android side.

@german-wings you have encountered a separate issue that was on our servers. It has now been fixed.

german-wings commented 5 years ago

Hey thanks for the response , @jsflax Could you elaborate on the kind of issue I was facing , My Application was very simple and the database schema was too , so I just hopped on to firebase. But Stitch is good , any information on that would be helpful. Thank You

jsflax commented 5 years ago

Hi @german-wings ,

I actually meant to ping @argz777 , @jpbulbulian were facing a separate issue where the server was not properly parsing the payload we were passing to it. It is fixed now.

jpbulbulian commented 5 years ago

Thank you @jsflax It's working well right now!