question

pavan avatar image
pavan asked sam Deactivated commented

Hi we trying to get auth_token using the example provided for android but no success

we using https://github.com/clover/android-exa... to get auth token via oauth in android but we are unable to get token we did all steps provided in example we are able to login in clover but not getting access token .

i am passing "redirect_uri=http://localhost" as it is

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    super.setContentView(R.layout.activity_webview);

    // The URL that will fetch the Access Token, Merchant ID, and Employee ID
    String url = "https://clover.com/oauth/authorize" +
            "?client_id=" + "myappid" +
            "&response_type=token" +
            "&redirect_uri=http://localhost";

    // Creates the WebView
    webView = (WebView) findViewById(R.id.webView);
    webView.getSettings().setJavaScriptEnabled(true);
    webView.setWebViewClient(new WebViewClient() {
        public void onPageStarted(WebView view, String url, Bitmap favicon) {
            // Parses the fetched URL
            String accessTokenFragment = "#access_token=";
            String merchantIdFragment = "&merchant_id=";
            String employeeIdFragment = "&employee_id=";

            int accessTokenStart = url.indexOf(accessTokenFragment);
            int merchantIdStart = url.indexOf(merchantIdFragment);
            int employeeIdStart = url.indexOf(employeeIdFragment);
            if (accessTokenStart > -1) {
                String accessToken = url.substring(accessTokenStart + accessTokenFragment.length(), merchantIdStart);
                String merchantId = url.substring(merchantIdStart + merchantIdFragment.length(), employeeIdStart);
                String employeeId = url.substring(employeeIdStart + employeeIdFragment.length(), url.length());

                // Sends the info back to the MainActivity
                Intent output = new Intent();
                output.putExtra(selectFunction.ACCESS_TOKEN_KEY, accessToken);
                output.putExtra(selectFunction.MERCHANT_ID_KEY, merchantId);
                output.putExtra(selectFunction.EMPLOYEE_ID_KEY, employeeId);
                setResult(RESULT_OK, output);
                finish();
            }
        }
    });
    // Loads the WebView
    webView.loadUrl(url);
}

getting resulted url is https://www.clover.com/appmarket/m/MyMerchantID/apps/MyAppID?redirect_uri=http%253A%252F%252Flocalhost&response_type=token do i need any other setting or changes to get expected result anything missing please let me know

Update 6 May 2017 i am getting proper result now the problem i facing to parse accestoken from the url i am using code block from clover ouh example

String accessTokenFragment = "#access_token=";
                String merchantIdFragment = "&merchant_id=";
                String employeeIdFragment = "&employee_id=";

                int accessTokenStart = url.indexOf(accessTokenFragment);
                int merchantIdStart = url.indexOf(merchantIdFragment);///problem is here because of wrong or diff sequence returns -1
                int employeeIdStart = url.indexOf(employeeIdFragment);
                if (accessTokenStart > -1) {
                    String accessToken = url.substring(accessTokenStart + accessTokenFragment.length(), merchantIdStart);
                    String merchantId = url.substring(merchantIdStart + merchantIdFragment.length(), employeeIdStart);
                    String employeeId = url.substring(employeeIdStart + employeeIdFragment.length(), url.length());

                    // Sends the info back to the MainActivity
                    Intent output = new Intent();
                    output.putExtra(selectFunction.ACCESS_TOKEN_KEY, accessToken);
                    output.putExtra(selectFunction.MERCHANT_ID_KEY, merchantId);
                    output.putExtra(selectFunction.EMPLOYEE_ID_KEY, employeeId);
                    setResult(RESULT_OK, output);
                    finish();
                }

Here is the url i am getting

http://localhost:25110/Login.aspx?merchant_id=MYMERCHANTID&employee_id=MYEMPID&client_id=MYCLIENTID#access_token=MYACCESSTOKEN

so question is the sequence for all keys fixed? i dont think so according to mine scenario so how to get access token from that url? any help will be appericiated

OAuth
3 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.

sam avatar image sam commented ·

Is there a particular reason for getting the OAuth token through web rather than using the device token?: token = CloverAuth.authenticate(getBaseContext(), CloverAccount.getAccount(getBaseContext()), true, 1000l, TimeUnit.MILLISECONDS).authToken;

0 Likes 0 ·
pavan avatar image pavan commented ·

@Sam that was we trying when it failing to create on station but using method u suggested it is working now thanks

0 Likes 0 ·
pavan avatar image pavan commented ·

@Sam for my understanding or future use i am trying to get access token from url here i used the example from clover as it in question but the sequence i getting in url is diff than the example so logic to getting access token is failing and app getting crashed. i edited the question.

0 Likes 0 ·

1 Answer

pavan avatar image
pavan answered

the problem here that i missing two things 1. redirect url need to be added in app web url form clover developer dashboard. 2. for first time after navigating to app via outh u have to click on install button then we get the expected result

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