我无法获得与我共享的内容的所有者的电子邮件地址

问题描述

我可以从A DriveItem的shared.owner.user.id获取所有者的ID。

但是,当尝试获取id的电子邮件地址时,它将返回原始用户之一。

我已经尝试过几次更改权限(User.Read,User.BasicRead,User.Read.All)

它不起作用。

我认为这是一个错误。

环境是

android {
    compileSdkVersion 28
    buildToolsVersion "29.0.3"
    defaultConfig {
        applicationId "mytest"
        minSdkVersion 21
        targetSdkVersion 28
....
    }
.....
}

依赖项是

    implementation 'com.microsoft.identity.client:msal:1.4.+'
    implementation 'com.microsoft.graph:msgraph-sdk-android:1.7.+'
    implementation("com.squareup.okhttp3:okhttp:4.7.2")

示例代码为

package mytest;


import android.app.Activity;
import android.os.Bundle;
import android.os.SystemClock;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.microsoft.graph.authentication.IAuthenticationProvider;
import com.microsoft.graph.concurrency.DefaultExecutors;
import com.microsoft.graph.extensions.Drive;
import com.microsoft.graph.extensions.DriveItem;
import com.microsoft.graph.extensions.DriveItemCollectionPage;
import com.microsoft.graph.extensions.GraphServiceClient;
import com.microsoft.graph.extensions.IDriveCollectionPage;
import com.microsoft.graph.extensions.IDriveItemCollectionPage;
import com.microsoft.graph.extensions.IDriveSharedWithMeCollectionPage;
import com.microsoft.graph.extensions.IGraphServiceClient;
import com.microsoft.graph.extensions.ISharedDriveItemCollectionPage;
import com.microsoft.graph.extensions.IUserRequest;
import com.microsoft.graph.extensions.User;
import com.microsoft.graph.generated.BaseDriveItemCollectionPage;
import com.microsoft.graph.generated.BaseDriveItemCollectionRequest;
import com.microsoft.graph.generated.BaseDriveItemCollectionResponse;
import com.microsoft.graph.generated.IBaseDriveSharedWithMeCollectionRequest;
import com.microsoft.graph.generated.IBaseDriveSharedWithMeCollectionRequestBuilder;
import com.microsoft.graph.generated.IBaseSharedDriveItemCollectionRequest;
import com.microsoft.graph.generated.IBaseSharedDriveItemCollectionRequestBuilder;
import com.microsoft.graph.http.DefaultHttpProvider;
import com.microsoft.graph.http.IHttpRequest;
import com.microsoft.graph.logger.DefaultLogger;
import com.microsoft.graph.options.HeaderOption;
import com.microsoft.graph.options.Option;
import com.microsoft.graph.serializer.DefaultSerializer;
import com.microsoft.identity.client.AuthenticationCallback;
import com.microsoft.identity.client.IAccount;
import com.microsoft.identity.client.IAuthenticationResult;
import com.microsoft.identity.client.IPublicClientApplication;
import com.microsoft.identity.client.ISingleAccountPublicClientApplication;
import com.microsoft.identity.client.PublicClientApplication;
import com.microsoft.identity.client.SilentAuthenticationCallback;
import com.microsoft.identity.client.SingleAccountPublicClientApplication;
import com.microsoft.identity.client.exception.MsalClientException;
import com.microsoft.identity.client.exception.MsalException;
import com.microsoft.identity.client.exception.MsalServiceException;
import com.microsoft.identity.client.exception.MsalUiRequiredException;
import com.microsoft.identity.common.adal.internal.cache.StorageHelper;
import com.microsoft.identity.common.internal.cache.SharedPreferencesFileManager;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;

public class MainActivity extends Activity {

    ISingleAccountPublicClientApplication mSingleAccountApp;
    IAccount mAccount;

    TextView log;

//    final String defaultGraphResourceUrl = "https://graph.microsoft.com/v1.0/me";
    final String[] scope = {"user.read","files.readwrite.all","email"};//lower case


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        PublicClientApplication.createSingleAccountPublicClientApplication(this,R.raw.ms_auth_config,new IPublicClientApplication.ISingleAccountApplicationCreatedListener() {
                    @Override
                    public void onCreated(ISingleAccountPublicClientApplication application) {
                        /**
                         * This test app assumes that the app is only going to support one account.
                         * This requires "account_mode" : "SINGLE" in the config json file.
                         **/
                        mSingleAccountApp = application;

                    }

                    @Override
                    public void onError(MsalException exception) {
                        Log.e("P2","error",exception);
                    }
                });

        log = findViewById(R.id.log);

        final Button loginS = findViewById(R.id.loginS);
        loginS.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                log.setText("logining... sliently");
                mSingleAccountApp.acquireTokenSilentAsync(scope,mAccount.getAuthority(),getAuthSilentCallback());
            }
        });
        final Button logout = findViewById(R.id.logout);
        logout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                log.setText("logouting...");
                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        try {

                            SharedPreferencesFileManager sf = new SharedPreferencesFileManager(MainActivity.this,SingleAccountPublicClientApplication.SINGLE_ACCOUNT_CREDENTIAL_SHARED_PREFERENCES,new StorageHelper(MainActivity.this));
                            Log.e("P2","logout:" + sf.getAll());


                            mSingleAccountApp.signOut();

                            runOnUiThread(new Runnable() {
                                @Override
                                public void run() {
                                    log.setText("logout...done");
                                }
                            });
                        } catch (Exception e) {
                            Log.e("P2",e);
                            log.setText("logout...error");
                        }
                    }
                }).start();
            }
        });
        final Button login = findViewById(R.id.login);
        login.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                if (mSingleAccountApp == null) {
                    return;
                }

                log.setText("logining...");
                mSingleAccountApp.signIn(MainActivity.this,null,scope,getAuthInteractiveCallback());

            }
        });
    }

    String token;


    private void listDrives() {


        DefaultLogger defaultLogger = new DefaultLogger();
        DefaultSerializer serializer = new DefaultSerializer(defaultLogger);
        DefaultExecutors executor = new DefaultExecutors(defaultLogger);
        IAuthenticationProvider auth = new IAuthenticationProvider() {
            @Override
            public void authenticateRequest(IHttpRequest request) {

                request.addHeader("Authorization","Bearer " + token);

                Log.e("P2","method:" + request.getHttpMethod());
                Log.e("P2","request:" + request.getRequestUrl().toString());
                request.setUseCaches(false);
            }
        };
        IGraphServiceClient graphClient = new GraphServiceClient.Builder()
                .httpProvider(new DefaultHttpProvider(serializer,auth,executor,defaultLogger))
                .executors(executor)
                .serializer(serializer)
                .authenticationProvider(auth)
                .logger(defaultLogger)
                .buildClient();


        IBaseDriveSharedWithMeCollectionRequestBuilder request = graphClient.getDrive().getSharedWithMe();
        IDriveSharedWithMeCollectionPage page2 = request.buildRequest().get();

//        String lastId = null;
        for (DriveItem d : page2.getCurrentPage()) {
            Log.e("P2","shared:" + d.name + ",owner id:" + d.remoteItem.shared.owner.user.id);
            String lastId = d.remoteItem.shared.owner.user.id;
            ArrayList<Option> opts = new ArrayList<>();
            opts.add(new HeaderOption("request-id","mytest-"+ SystemClock.uptimeMillis()));
            IUserRequest ir = graphClient.getUsers(lastId).buildRequest(opts);

            User user = ir.get();
            Log.e("P2","sharedOwner:" + user.userPrincipalName + "," + user.mail + "," + lastId + "," + user.id);
        }




    }


    /**
     * Callback used in for silent acquireToken calls.
     */
    private SilentAuthenticationCallback getAuthSilentCallback() {
        return new SilentAuthenticationCallback() {

            @Override
            public void onSuccess(IAuthenticationResult authenticationResult) {

                log.setText("logining... sliently done");
                token = authenticationResult.getAccessToken();

                Log.e("P2","Successfully authenticated silently:");

                Log.e("P2","silent authenticated:" + token);
                Log.e("P2","silent authenticated:" + authenticationResult.getExpiresOn().toLocaleString());


                new Thread(new Runnable() {
                    @Override
                    public void run() {
//                        listDrives2();
//                        listDrives();
                    }
                }).start();

                /* Successfully got a token,use it to call a protected resource - MSGraph */
//                callGraphAPI(authenticationResult);
            }

            @Override
            public void onError(MsalException exception) {

                log.setText("logining... sliently error");
                /* Failed to acquireToken */
                Log.e("P2","Authentication failed: " + exception.getErrorCode() + "," + exception.getMessage());
//                displayError(exception);

                if (exception instanceof MsalClientException) {
                    /* Exception inside MSAL,more info inside MsalError.java */
                } else if (exception instanceof MsalServiceException) {
                    /* Exception when communicating with the STS,likely config issue */
                } else if (exception instanceof MsalUiRequiredException) {
                    /* Tokens expired or no session,retry with interactive */
                }
            }
        };
    }

    /**
     * Callback used for interactive request.
     * If succeeds we use the access token to call the Microsoft Graph.
     * Does not check cache.
     */
    private AuthenticationCallback getAuthInteractiveCallback() {
        return new AuthenticationCallback() {

            @Override
            public void onSuccess(IAuthenticationResult authenticationResult) {


                log.setText("logining... done");

                token = authenticationResult.getAccessToken();
                /* Successfully got a token,use it to call a protected resource - MSGraph */
                Log.e("P2","Successfully authenticated:" + token);
                Log.e("P2","Successfully authenticated:" + authenticationResult.getExpiresOn().toLocaleString());
                Log.e("P2","user:" + authenticationResult.getAccount().getUsername() + "," + authenticationResult.getAccount().getId());
//                Log.e("P2","ID Token: " + authenticationResult.getAccount().getClaims().get("id_token"));
//                Log.e("P2","header:" + authenticationResult.getAuthenticationScheme());

                /* Update account */
                mAccount = authenticationResult.getAccount();


//                Log.e("P2","header:" + mAccount.getAuthority() + "," + mAccount.getIdToken());

//                SharedPreferencesFileManager sf = new SharedPreferencesFileManager(MainActivity.this,//                        SingleAccountPublicClientApplication.SINGLE_ACCOUNT_CREDENTIAL_SHARED_PREFERENCES,//                        new StorageHelper(MainActivity.this));
//                Log.e("P2","getAll:" + sf.getAll());


                new Thread(new Runnable() {
                    @Override
                    public void run() {
                        listDrives();
//                        listDrives2();
                    }
                }).start();
//                updateUI();

                /* call graph */


//                callGraphAPI(authenticationResult);
            }

            @Override
            public void onError(MsalException exception) {

                log.setText("logining... error");

                /* Failed to acquireToken */
                Log.e("P2"," + exception.getMessage());


                if (exception instanceof MsalClientException) {
                    /* Exception inside MSAL,likely config issue */
                }
            }

            @Override
            public void onCancel() {
                /* User canceled the authentication */
                Log.e("P2","User cancelled login.");
            }
        };
    }


}


样本结果是

2020-09-07 14:59:04.245 3092-3092 E/P2: Successfully authenticated:EwB4A8l6BAAUO9chh8cJscQLmU+LSWpbnr0v...
2020-09-07 14:59:04.250 3092-3092 E/P2: Successfully authenticated:2020. 9. 7. 오후 3:59:01
2020-09-07 14:59:04.252 3092-3092 E/P2: user:mytestaccount@gmail.com,00000000-0000-0000-400e-580b929...
2020-09-07 14:59:04.307 3092-3758 E/P2: method:GET
2020-09-07 14:59:04.307 3092-3758 E/P2: request:https://graph.microsoft.com/v1.0/drive/microsoft.graph.sharedWithMe
2020-09-07 14:59:05.225 3092-3758 E/P2: shared:mytestfolder1,owner id:4bcbc2310d39d3ec
2020-09-07 14:59:05.237 3092-3758 E/P2: method:GET
2020-09-07 14:59:05.237 3092-3758 E/P2: request:https://graph.microsoft.com/v1.0/users/4bcbc2310d39d3ec
2020-09-07 14:59:05.596 3092-3758 E/P2: sharedOwner:mytestaccount@gmail.com,4bcbc2310d39d3ec,400e580b9296e15d
2020-09-07 14:59:05.596 3092-3758 E/P2: shared:mytestfolder2,owner id:4168cffb3d4e4f4c
2020-09-07 14:59:05.596 3092-3758 E/P2: method:GET
2020-09-07 14:59:05.596 3092-3758 E/P2: request:https://graph.microsoft.com/v1.0/users/4168cffb3d4e4f4c
2020-09-07 14:59:05.987 3092-3758 E/P2: sharedOwner:mytestaccount@gmail.com,4168cffb3d4e4f4c,400e580b9296e15d

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

依赖报错 idea导入项目后依赖报错,解决方案:https://blog....
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下...
错误1:gradle项目控制台输出为乱码 # 解决方案:https://bl...
错误还原:在查询的过程中,传入的workType为0时,该条件不起...
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct...