简体   繁体   中英

Azure Mobile Services native android login

I have an Easy API and an Easy Table running on the Azure Mobile Services. Both of these require authentication to be accessed. I have also set up Google Authentication. Now when I try to connect to the Mobile Service using the android App, I get a WebView of Google Login Page, while I have already authenticated the user when the app started. I followed the following guide to setup the authentication on android and test posting a single object to the table.

https://azure.microsoft.com/en-in/documentation/articles/mobile-services-android-how-to-use-client-library/#authentication

Here is my code for the authentication and addition to the table. Also the data is not being inserted in the table after logging in using this WebView. It fails silently without any errors or warnings.

Creating the Client. No API key is provided on the Azure portal, and I read here that one is not required now. But the SDK is not updated with this so I passed an empty string.

try {
    mClient = new MobileServiceClient(
            "https://memoriesapp.azurewebsites.net",
            "",  
            this
    );
} catch (MalformedURLException e) {
    e.printStackTrace();
}


final MobileServiceTable<Trip> mTripTable = mClient.getTable(Trip.class);
// Login using the Google provider.

ListenableFuture<MobileServiceUser> mLogin = mClient.login(MobileServiceAuthenticationProvider.Google);

Futures.addCallback(mLogin, new FutureCallback<MobileServiceUser>() {
    @Override
    public void onFailure(Throwable exc) {

    }

    @Override
    public void onSuccess(MobileServiceUser user) {
        Snackbar.make(findViewById(R.id.new_trip_layout),String.format(
                "You are now logged in - %1$2s",
                user.getUserId()), Snackbar.LENGTH_LONG);
    }
});

new AsyncTask<Void, Void, Void>() {

    @Override
    protected Void doInBackground(Void... params) {
        try {
            mTripTable.insert(trip1).get();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } catch (ExecutionException e) {
            e.printStackTrace();
        }
        return null;
    }
}.execute();

This is what I get when I go to that activity:

Imgur

Sounds like you are mixing and matching Azure App Service with Azure Mobile Services.

Mobile Services do require an API key. App Services do not.

You are pointing to documentation for Mobile Services - if you are following that tutorial, you need to add your API key. If you do not, the backend will return a 401 Unauthorized and you are likely to get redirected to a login page.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM