初始OAuth Android之后的OneDrive API

时间:2015-07-06 03:44:45

标签: android oauth onedrive

我目前正在使用此处提供的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完成我想做的事?

谢谢,

PARTH

1 个答案:

答案 0 :(得分:1)

我没有查看您的代码,但这对我有用:

在我的设置活动中:

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

}