m0bl
m0bl

Reputation: 612

Getting unauthorized when saving parse installation record on Android devices

PROBLEM SOLVED

Note that my parse config in index.js did not contain clientKey, but my app IS passing a value for clientKey.

Here's the updated portion of the index.js with the clientKey added:

var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://mymongodb,
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myappid',
masterKey: process.env.MASTER_KEY || 'mymasterkey',
restAPIKey: process.env.REST_API_KEY || 'myrestapikey',
clientKey: 'myclientkey'
....

A second error I made when testing was thinking that iOS was working with respect to creating new installation records - I was incorrect, it was not working... I believe had tested an upgrade flow, and the old Parse version of the app created the installation record, thereby enabling the new parse-server version to send push to iOS. Android, however, would not work in that manner b/c the installation record required SenderID, so the parse-server version of Android was unable to work after upgrading (like iOS was).


ORIGINAL QUESTION BELOW

I've followed all the migration instructions to switch over from Parse to parse-server. Parse-server is working fine for iOS.

With Android, I am sometimes getting unauthorized when trying to save the parse installation. I'll consistently get unauthorized on a particular device until it works once.

Once a the installation record save succeeds, all subsequent calls to save the installation record seem to work.

Here's the relevant Android code:

String parseAppId = getString(R.string.parse_app_id);
String parseClientKey = getString(R.string.parse_client_key);
String parseServer = getString(R.string.parse_server);

Parse.initialize(new Parse.Configuration.Builder(this)
     .applicationId(parseAppId)
     .clientKey(parseClientKey)
     .server(parseServer)
     .build()
);

ParseInstallation parseInstallation = ParseInstallation.getCurrentInstallation();
parseInstallation.saveEventually(new SaveCallback() {
    @Override
    public void done(ParseException e) {
                // e = com.parse.ParseRequest$ParseRequestException: unauthorized
    }
}

Here's my server config in index.js (actual values redacted):

var api = new ParseServer({
    databaseURI: databaseUri || 'mongodb://mymongodb,
    cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
    appId: process.env.APP_ID || 'myappid',
    masterKey: process.env.MASTER_KEY || 'mymasterkey',
    restAPIKey: process.env.REST_API_KEY || 'myrestapikey',
    serverURL: process.env.SERVER_URL || 'https://my.server.com/parse',
    liveQuery: {
        classNames: ["Posts", "Comments"] // List of classes to support for query subscriptions
    },
    push: {
        android: {
            senderId: 'mysenderid',
            apiKey: 'myapikey'
        },
        ios: {
            pfx: __dirname + '/certs/myp12file.p12',
            bundleId: 'my.bundle.id',
            production: true
        }
    },
    verbose: true
});

I've verified and re-verified a million times the AppId, ClientKey, and Server settings... they're identical to what I have in the parse config in the index.js file, and iOS and Android values are identical (copied and pasted), with the exception that Android has an extra trailing slash "/" at the end of the server URL (as noted in the setup docs and other SO questions).

One similar issue I found was this one:https://github.com/ParsePlatform/parse-server/issues/635

But since my AppId matches, that can't be causing what's happening.

This site notes that when the AppId is updated, it can cause the 403 response because parse caches old AppId and ClientKey values:

http://androidetc.com/android/tutorial_parse_server_android_heroku_mongodb/

However, the client key and AppId are unchanged from Parse. Also, I'm getting this with new emulators now as well.

I was also previously using 'saveInBackground' instead of 'saveEventually', and saw a note that said the unauthorized error might be fixed by it.

I'm using the current android sdk for parse: (from my build.gradle)

compile 'com.parse:parse-android:1.13.1'

I also have two emulators that are able to save the parse installation when running with the same code, but fail if I try to save with any other values (I separated the additional values into a separate call that I'm doing after the first installation save attempt - I wanted to see if it made a difference if I tried to save with or without the additional values... and for some devices, it does?):

parseInstallation.put("email", email);
parseInstallation.put("userID", userID);
parseInstallation.put("campusID", campusID);
parseInstallation.put("GCMSenderId", "...senderid...");

(Yes I realize that I should store those values in another table - TBD later)

... deleted irrelevant info from initial post

Upvotes: 3

Views: 696

Answers (1)

m0bl
m0bl

Reputation: 612

Note that my parse config in index.js did not contain clientKey, but my app IS passing a value for clientKey.

Here's the updated portion of the index.js with the clientKey added:

var api = new ParseServer({
databaseURI: databaseUri || 'mongodb://mymongodb,
cloud: process.env.CLOUD_CODE_MAIN || __dirname + '/cloud/main.js',
appId: process.env.APP_ID || 'myappid',
masterKey: process.env.MASTER_KEY || 'mymasterkey',
restAPIKey: process.env.REST_API_KEY || 'myrestapikey',
clientKey: 'myclientkey'

A second error I made when testing was thinking that iOS was working with respect to creating new installation records - I was incorrect, it was not working... I believe had tested an upgrade flow with iOS, and the old Parse version of the app created the installation record, thereby enabling the new parse-server version to send push to iOS. Android, however, would not work in that manner b/c the installation record required SenderID, so the parse-server version of Android was unable to work after upgrading (like iOS was).

Upvotes: 0

Related Questions