question

Robin Walmsley avatar image
Robin Walmsley asked Robin Walmsley commented

Auth token not working in android app

I continued to be plagued with a highly frustrating problem thats driving me crazy.

I have a problem whereby the token auth_token issued by my Android App is failing in my web-app.

This is the line of code from the Android App.

webview.loadUrl("https:/domainname.com/cloverlogin?authtoken=" + result.authToken + "&merchantid=" + result.merchantId +"&appid=" + result.appId);

Attached are two images I've captured from Genymotion. The first is when I use the authToken passed from the Android App which results in an error 401 when I attempt an API call. The second is when I hard code the authtoken to be the one created in Sandbox against this merchant. With the second image you can see the authtoken I've used is not the same as the one passed to my PHP endpoint . You can clearly see the auth_token passed from the Android app is failing with an error 401.

I have set all the permissions to ticked against the app and I have uninstalled and reinstalled the app several times. I have done this on both the Emulator and the Clover Station with the same results.

Any ideas?

Robin

image description image description

EDIT

Here is all the code from my Android App

MainAcvtivity.java

package com.fashionservers.www.fsinventorymanagement;

import android.accounts.Account;
import android.content.Context;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.Window;

import com.clover.sdk.util.CloverAccount;
import com.clover.sdk.util.CloverAuth;
import com.clover.sdk.v1.app.AppNotification;
import com.clover.sdk.v1.app.AppNotificationReceiver;

public class MainActivity extends AppCompatActivity {

private Account mAccount;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);

    setContentView(R.layout.activity_main);

}

@Override
protected void onResume() {
    super.onResume();

    // Retrieve the Clover account
    if (mAccount == null) {
        mAccount = CloverAccount.getAccount(this);

        if (mAccount == null) {
            finish();
        }

        // Use this account to get the access token (and other Clover authentication data)
        getCloverAuth();
    }
}

private void getCloverAuth() {
    // This needs to be done on a background thread
    new AsyncTask<Void, Void, CloverAuth.AuthResult>() {
        @Override
        protected CloverAuth.AuthResult doInBackground(Void... params) {
            try {
                return CloverAuth.authenticate(MainActivity.this, mAccount);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }

        @Override
        protected void onPostExecute(CloverAuth.AuthResult result) {
            WebView webview = new WebView(getBaseContext());
            setContentView(webview);
            webview.setWebViewClient(new WebViewClient());
            webview.getSettings().setJavaScriptEnabled(true);

            webview.loadUrl("https://www.fashionservers.com/stores/clover_login?auth_token=" + result.authToken  + "&merchant_id=" + result.merchantId +"&app_id=" + result.appId);

        }
    }.execute();
}

}
AndroidManifest


<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.fashionservers.www.fsinventorymanagement">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.GET_ACCOUNTS"/>
<uses-permission android:name="android.permission.USE_CREDENTIALS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="Fashion Inventory"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:minSdkVersion="17"
    android:versionCode="4"
    android:versionName="4"
    android:theme="@style/Theme.AppCompat.NoActionBar">


    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>

</application>

</manifest>

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.fashionservers.www.fsinventorymanagement.MainActivity">

<WebView
    android:id="@+id/webView"
    android:layout_width="696dp"
    android:layout_height="447dp"
    android:layout_marginRight="432dp"
    android:layout_marginTop="183dp"
    android:visibility="visible"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />


</android.support.constraint.ConstraintLayout>
Auth
2 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

pavan avatar image pavan commented ·

how you fetching auth token from android using oAuth or through clover android sdk?

0 Likes 0 ·
Robin Walmsley avatar image Robin Walmsley commented ·

Its from the Clover Android SDK .. I have added all the code from my Android APP back into my original question. Thanks

0 Likes 0 ·
pavan avatar image
pavan answered Robin Walmsley commented

that's the problem buddy which i also getting it is clover known issue It sounds like you're not receiving a valid token from your device due to the known issue clover have when the app is uninstalled then installed. you can check this

replace

CloverAuth.authenticate(MainActivity.this, mAccount);

with this in your code

CloverAuth.authenticate(MainActivity.this,mAccount,true,70L, TimeUnit.SECONDS);

where forceValidateToken will get you the latest token timeout is for max wait time for response TimeUnit will be anything you want seconds, minute etc.

check this too for reference https://devask.clover.com/question/77...

10 comments
10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Robin Walmsley avatar image Robin Walmsley commented ·

I'll give it a try, that sounds helpful. I am a php/html/css/javascript developer, and I have zero experience with Android, the code above was kindly written for me by Sam from the clover support team, so I have been groping around in the dark with this one for a few weeks. Where are you based?

0 Likes 0 ·
pavan avatar image pavan commented ·

m from India, i will update statement for you just paste that :)

0 Likes 0 ·
Robin Walmsley avatar image Robin Walmsley commented ·

Pavan, you;re a star thanks for your help :)

0 Likes 0 ·
pavan avatar image pavan commented ·

welcome, check edited answer

0 Likes 0 ·
Robin Walmsley avatar image Robin Walmsley commented ·

OK are you saying .. replace my code

CloverAuth.authenticate(MainActivity.this, mAccount);

with this code

CloverAuth.authenticate(MainActivity.this,mAccount,true,70L, TimeUnit.SECONDS);

... do I need this stil? authenticate(Activity activity, Account account, boolean forceValidateToken, Long timeout, TimeUnit unit)

0 Likes 0 ·
Show more comments
Robin Walmsley avatar image
Robin Walmsley answered

Thanks to Pavan, his suggestion has worked.

10 |2000

Up to 2 attachments (including images) can be used with a maximum of 512.0 KiB each and 1.0 MiB total.

Welcome to the
Clover Developer Community