Search code examples
iosauthenticationsnapchatsnapkit

Snapchat SnapKit Login on iOS 'fetchUserDataWithQuery' always fails with SCOAuth2ClientErrorDomain 403


I'm trying out Snpachat's SnapKit login api, and I've setup my project as described in the documentation/guide. I've allowed the use of all the scopes, i.e. external id, display name and bitmoji in the dashboard and added the required fields in the .plist of my app.

The login and authentication proceed normal and return successfully, but when I try to fetch user data, that request fails every time with the SCOAuth2ClientErrorDomain error.

I'm using the snippet provided within the guide (although that code has a typo and doesn't build as is so I doubt in the validity of that code):

[SCSDKLoginClient loginFromViewController:self completion:^(BOOL success, NSError * _Nullable error) {
    NSString *graphQLQuery = [@"{me{displayName, bitmoji{avatar}, externalId}}" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

    NSDictionary *variables = @{@"page": @"bitmoji"};

    [SCSDKLoginClient fetchUserDataWithQuery:graphQLQuery
                                  variables:variables
                                    success:^(NSDictionary *resources) {
                                        NSDictionary *data = resources[@"data"];
                                        NSDictionary *me = data[@"me"];
                                        NSString *displayName = me[@"displayName"];
                                        NSDictionary *bitmoji = me[@"bitmoji"];
                                        NSString *bitmojiAvatarUrl = bitmoji[@"avatar"];
                                    } failure:^(NSError * error, BOOL isUserLoggedOut) {
                                        // handle error as appropriate
                                    }];
}];

I've even tried configurin my app without the bitmoji and tried the request without it, it still fails.

[SCSDKLoginClient loginFromViewController:self completion:^(BOOL success, NSError * _Nullable error) {
        NSString *graphQLQuery = [@"{me{displayName, externalId}}" stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

        [SCSDKLoginClient fetchUserDataWithQuery:graphQLQuery
                                      variables:nil
                                        success:^(NSDictionary *resources) {
                                            NSDictionary *data = resources[@"data"];
                                            NSDictionary *me = data[@"me"];
                                            NSString *displayName = me[@"displayName"];
                                        } failure:^(NSError * error, BOOL isUserLoggedOut) {
                                            // handle error as appropriate
                                        }];
}];

Anyone have any idea what I might be doing wrong?


Solution

  • Ok, I got it working.

    First of all, I re-added the user I was testing with to the demo users in the developer portal. After that, the SCOAuth2ClientErrorDomain error was gone and I was getting the success callback.

    But, the response data was an error in the query string. The problem is that they're using a deprecated method stringByAddingPercentEscapesUsingEncoding. I'm not sure what the exact problem is but I've just sent the raw string as a query and I got a valid response.

    UPDATE: I think encoding here is not needed. It doesn't make sense for the user of the api to encode the query. The api should handle it internally, and I think that is what might be happening here. So you probably get a double encoded query which then isn't encoded properly and is invalid. I tested encoding with non-deprecated methods for URL queries and it still didn't work. A raw query string is the way to go.