Search code examples
iosflutterapple-sign-inapple-oauth2

Flutter: Server side apple sign in error ( client_id mismatch )


We have a website with Apple login. The App ID and service ID for this login are com.website.login and com.website.service.login respectively and users are able to log in without any issues.

Now, we are building a mobile app and would like to authenticate the user on the server. To do this, I am using sign_in_with_apple package (link). We are using the same clientId that we are using over our website - com.website.service.login. Here's a code snippet of the same:

 credentials = await SignInWithApple.getAppleIDCredential(
      scopes: scopes,
      webAuthenticationOptions: WebAuthenticationOptions(
        clientId: 'com.website.service.login',   
        redirectUri: Uri.parse('https://website.com/apple/callback'),
      ),
      state: state,
    );

When I verify the code using the post request to my callback, I get the error - client_id mismatch. The code was not issued to com.website.service.login.

Any help is greatly appreciated. Thanks!


Solution

  • This maybe not exact answer, but in my case it helped to change serviceId to the app bundleId on server side for iOS application.

    The example below, which is included in the sign_in_with_apple package, is also showing that part (in my case, I completely missed it).

    app.post("/sign_in_with_apple", async (request, response) => {
      const auth = new AppleAuth(
        {
          // use the bundle ID as client ID for native apps, else use the service ID for web-auth flows
          // https://forums.developer.apple.com/thread/118135
          client_id:
            request.query.useBundleId === "true"
              ? process.env.BUNDLE_ID
              : process.env.SERVICE_ID,
          team_id: process.env.TEAM_ID,
          redirect_uri:
            "https://flutter-sign-in-with-apple-example.glitch.me/callbacks/sign_in_with_apple", // does not matter here, as this is already the callback that verifies the token after the redirection
          key_id: process.env.KEY_ID
        },
        process.env.KEY_CONTENTS.replace(/\|/g, "\n"),
        "text"
      );
    }

    Source: https://glitch.com/~flutter-sign-in-with-apple-example