1

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

asked 2017-05-04 11:20:34 -0500

Pavan gravatar image

updated 2017-05-06 03:16:59 -0500

Comments

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;

Sam gravatar image
Sam 2017-05-05 19:30:07 -0500

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

Pavan gravatar image
Pavan 2017-05-06 01:33:58 -0500

@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.

Pavan gravatar image
Pavan 2017-05-06 03:10:57 -0500