OneDrive API 在初始 OAuth Android 之后

OneDrive API after initial OAuth Android

我目前正在使用此处提供的 One Drive Android API:

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

我能够成功执行 OAuth 以授予我的应用程序权限。

但是,我无法利用 API 前进。我不断收到 401 权限被拒绝错误。

    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);

上面的能用,后面的我就不能用了

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;

}

我怎样才能让 API 做我想做的事?

谢谢,

帕特

我没有仔细阅读您的代码,但这是对我有用的代码:

在我的设置中 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
            }
        });

在其他地方使用/消费 API:

    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) {
            }
        });

还有我的工厂(你也可以内联这段代码):

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;
    }

}