android - 初始 OAuth Android 之后的 OneDrive API

标签 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 做我想做的事?

谢谢,

部分

最佳答案

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

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

}

关于android - 初始 OAuth Android 之后的 OneDrive API,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31237478/

相关文章:

ios - OneDrive SDK - IOS 获取特殊文件夹

java - onClickListener 是否在其内部的所有代码都在 Android 中运行后才更新 UI?

php - 创建 Google 联系人 --> 为什么只有 "NAME"没有插入新联系人?

oauth - WebSphere Docker OAuth

ruby-on-rails - 如何编写需要 OAuth2 访问 token 的 RSpec 测试?

android-studio - Gradle 错误 : Path is not a readable directory for an Android Project saved in OneDrive

microsoft-graph-api - Filter 和 orderby 查询参数不适用于一个驱动图 api

javascript - 有没有办法通过javascript找出Chrome的媒体设置环境?

android - RenderScript 支持库 V8 编译失败

android - 如何从 android 微调器上的数据库中检索数据?