Search code examples
androidoauthonedrive

OneDrive API after initial OAuth Android


I'm currently using the One Drive Android API available here:

https://github.com/OneDrive/onedrive-explorer-android

I am able to successfully perform OAuth to give permissions to my application.

However, I am unable to make use of the API moving forward. I keep receiving 401 permission denied errors.

    public static final List<String> SCOPES = Arrays.asList("wl.signin", "onedrive.readwrite");


    mAuthClient = new AuthClient(this, OneDriveOAuthConfig.getInstance(), OneDriveController.ONEDRIVE_CLIENT_ID);
    mAuthClient.login(this, OneDriveController.SCOPES, mAuthListener);

The above works, but I cannot use the following later

public void setAuthClient()
{
    if (mAuthClient == null)
    {
        mAuthClient = new AuthClient(getContext(), OneDriveOAuthConfig.getInstance(), ONEDRIVE_CLIENT_ID);
        mAuthClient.initialize(SCOPES, mAuthListener, null, mAccount.getToken());
    }
}

AuthListener mAuthListener = new AuthListener() {
    @Override
    public void onAuthComplete(AuthStatus status, AuthSession session, Object userState) {

    }

    @Override
    public void onAuthError(AuthException exception, Object userState) {
        exception.printStackTrace();

    }
};

/**
 * Get an instance of the OneDrive service
 *
 * @return The OneDrive Service
 */
synchronized IOneDriveService getOneDriveService() {
    if (mODConnection == null) {
        setAuthClient();
        final ODConnection connection = new ODConnection(mAuthClient);
        connection.setVerboseLogcatOutput(true);
        mODConnection = connection.getService();
    }
    return mODConnection;
}

@Override
public double getRemainingSpace() {

    getOneDriveService().getDrive(new Callback<Drive>() {
        @Override
        public void success(Drive drive, Response response) {
            remaining_space = drive.Quota.Remaining.doubleValue();
        }

        @Override
        public void failure(RetrofitError error) {
            error.printStackTrace();
        }
    });

    return remaining_space;

}

How exactly can I get the API to do what I want to do?

Thanks,

Parth


Solution

  • I didn't go through your code but here is what works for me:

    In my setup activity:

        AuthClient authClient = AuthClientFactory.getAuthClient(this);
        authClient.login(
            this,
            Arrays.asList("wl.signin", "wl.offline_access", "onedrive.readwrite"),
            new AuthListener() {
                @Override
                public void onAuthComplete(AuthStatus status, AuthSession session, Object userState) {
                    Log.d(TAG, "authorized " + status);
                    if (status == AuthStatus.CONNECTED) {
                        // you're good
                    } else {
                        Log.e(TAG, "bad status " + status.name());
                        // handle error
                    }
                }
    
                @Override
                public void onAuthError(AuthException exception, Object userState) {
                    Log.e(TAG, "failed", exception);
                    // handle error
                }
            });
    

    Using / consuming the API somewhere else:

        authClient = AuthClientFactory.getAuthClient(context);
        authClient.initialize(new AuthListener() {
            @Override
            public void onAuthComplete(AuthStatus status, AuthSession session, Object userState) {
    
            }
    
            @Override
            public void onAuthError(AuthException exception, Object userState) {
                Log.e(TAG, "connect failed", exception);
            }
        });
    
            final IOneDriveService service = OneDriveServiceFactory.getService(authClient);
    
            service.yourRetrofitInterfaceFunctionHere(yourParamsHere, new Callback<YourCallbackTypeHere>() {
                @Override
                public void success(Item item, Response response) {
                }
    
                @Override
                public void failure(RetrofitError error) {
                }
            });
    

    And my factory (you can also inline this code):

    package ****;
    
    import android.content.Context;
    
    import ***.OneDriveOAuthConfig;
    
    public class AuthClientFactory {
    
        private static AuthClient authClient;
    
        public static AuthClient getAuthClient(Context context) {
            if (authClient == null)
                authClient = new AuthClient(context, OneDriveOAuthConfig.getInstance(), "YOUR CLIENT ID");
            return authClient;
        }
    
    }