Two legged authentication Android SDK

Hello, I would like to clarify me how to use the Android SDK (cloud and setup) when it is implementing “two leggued authentication”. I have my own backend (using OAuth credentials of a client) to create and store customers access token. All this following the guide https://docs.particle.io/guide/how-to-build-a-product/authentication/#two-legged-authentication.

Now I’m building the mobile application using the SDK. The login of the mobile application is made against my own server. Once the client is logged the idea is to use the SDK for capturing variables, call functions and configure devices. Reading the documentation you need to create the strings which specifies “oauth_client_id”, “oauth_client_secret”, “organization_slug” and “product_slug” but How I can log into the Particle cloud with the SDK? I ask this because the method requires password eg particleCloud.logIn ("ido@particle.io", “l33tp4ssw0rd”);. When the customer is created in this type of authentication is not created with password:

curl -X POST -u “xx:xx” -d email=rr@gmail.com -d no_password=true https://api.particle.io/v1/orgs/hakken/customers

I think I’m misinterpreting something?
Thank you for your help

1 Like

It looks like there’s a particleCloud.setAccessToken(tokenString, expirationDate) method that will log the customer in with the access token you have created.

Thanks for answering

Even when using the method setAccessToken with a valid token if I invoke the setup library sends me to the login activity.

Any ideas?

@ruframapi if you’re still getting the login activity even after calling setAccessToken(), that’s a bug. I’m going to be refactoring some of this code very soon, so it should be fixed in the next couple weeks.

Here’s the github bug I’ve filed for this: https://github.com/spark/spark-setup-android/issues/13

Thank you, it is very important for us to use the Device Setup with 2-legged authentication. I wish you a happy coding .

Hey guys,

I’m running into the same problem… basically trying to figure out how to integrate two-leg authentication within an android app.

Can anyone shed some light on where I would even include the particleCloud.setAccessToken() function in relation to the sample apps (either SDK or Setup)?

Sorry if my question is too beginner but I’m just trying to figure out where to even get started with this. Any more insight into understanding the flow of the applications would be great

Thanks

@reado You can call ParticleCloud.setAccessToken() right after you initialize the SDK.

I’m working on a few TODOs for Electron setup right now, but after those are done I’m going to be working on auth issues, including a brief addition to the docs on how to integrate 2-leg auth. Thanks for your patience, everyone.

I just tried this and I wasn’t able to reproduce it. Can anyone here try cloning the cloud SDK repo and trying the example app?

Once you have the example app building, open up LoginActivity, comment out the logIn() call, and replace it with this:

// the date below is arbitrary.  you can copy & paste it into the example code
// without worrying about your actual token expiration date
sparkCloud.setAccessToken("YOUR ACCESS TOKEN HERE", new Date(1461456951753L)); 

…and then try whatever SDK calls you want to make after that, and see what you get?

Thanks!

@reado @ruframapi could either of you try to repro with the steps I gave above? Thanks!

@jensck As you said I did and added below code before button listener

ParticleCloud particleCloud = ParticleCloud.get(this); particleCloud.setAccessToken("ACCESS_TOKEN", new Date(1461456951753L)); System.out.println("Is loggedIn:" + particleCloud.isLoggedIn());

And it returned me false

So I’m able to get the login working, but getting the claiming code is failing.

Below is in my Activity to start device setup.

        Responses.LogInResponse response = null;
        try {
            response = new Responses.LogInResponse(
                    token.getLong("expires_in"),
                    token.getString("access_token"),
                    token.getString("token_type")
            );
        } catch (JSONException e) {
            e.printStackTrace();
        }

        ParticleAccessToken accessToken = ParticleAccessToken.fromNewSession(response);
        ParticleUser particleUser = ParticleUser.fromNewCredentials(mUserEmail, "placeholder");

        ParticleDeviceSetupLibrary.startDeviceSetup(this);

Here’s the failure I’m getting:

E/Async: Error calling API: activation code was empty
io.particle.android.sdk.cloud.ParticleCloudException
    at io.particle.android.sdk.cloud.ParticleCloud.generateClaimCodeForOrg(ParticleCloud.java:281)
    at io.particle.android.sdk.devicesetup.ui.GetReadyActivity$3.callApi(GetReadyActivity.java:114)
    at io.particle.android.sdk.devicesetup.ui.GetReadyActivity$3.callApi(GetReadyActivity.java:109)
    at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:105)
    at io.particle.android.sdk.utils.Async$AsyncApiWorker.doInBackground(Async.java:73)
    at android.os.AsyncTask$2.call(AsyncTask.java:288)
    at java.util.concurrent.FutureTask.run(FutureTask.java:237)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
    at java.lang.Thread.run(Thread.java:818)

Found the error.

I had the device set for private which requires a user code.

Also, it only works the second time the app is loaded. The ParticleUser.fromNewCredentials saves the user but does not update the user (odd).

We need a particleCloud.setUser(...) function.

@jensck – just created a pull request to add a setUser function

Sorry for the delay on this – I’ve had a couple urgent issues come up that I had to address first. I’ll be working on this tonight and will offer an update then.

Sorry Jensck for the delay answer, I was outside of my country.

I have the same issue reported by brandongoode, the claiming process fail.

Hi jensck

There is some progress on this issue?,

Thanks