gpt4 book ai didi

android - 初始 OAuth Android 之后的 OneDrive API

转载 作者:行者123 更新时间:2023-11-30 02:02:38 24 4
gpt4 key购买 nike

我目前正在使用此处提供的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com