Android outlook邮件REST API

Android outlook邮件REST API,android,email,outlook,outlook-api,Android,Email,Outlook,Outlook Api,我正在尝试访问我的outlook收件箱邮件 这是代码: Logger logger = LoggerFactory.getLogger(MainActivity.class); private static final String TAG = "MainActivity"; private static final String outlookBaseUrl = "https://outlook.office.com/api/v2.0"; private AuthenticationCon

我正在尝试访问我的outlook收件箱邮件

这是代码:

Logger logger = LoggerFactory.getLogger(MainActivity.class);

private static final String TAG = "MainActivity";
private static final String outlookBaseUrl = "https://outlook.office.com/api/v2.0";

private AuthenticationContext _authContext;
private DependencyResolver _resolver;
private OutlookClient _client;

private ListView lvMessages;
private String[] scopes = new String[]{"https://outlook.office.com/Mail.Read"};

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

    lvMessages = (ListView) findViewById(R.id.lvMessages);


    Futures.addCallback(logon(), new FutureCallback<Boolean>() {
        @Override
        public void onSuccess(Boolean result) {
            _client = new OutlookClient(outlookBaseUrl, _resolver);
            getMessages();
        }

        @Override
        public void onFailure(Throwable t) {
            logger.error("authentication failed", t);
        }
    });
}

public SettableFuture<Boolean> logon() {

    final SettableFuture<Boolean> result = SettableFuture.create();

    try {
        _authContext = new AuthenticationContext(this, getResources().getString(R.string.AADAuthority), true);
    } catch (Exception e) {
        Log.e(TAG, "Failed to initialize Authentication Context with error: " + e.getMessage());
        _authContext = null;
        result.setException(e);
    }

    if (_authContext != null) {
        _authContext.acquireToken(
                this,
                scopes,
                null,
                getResources().getString(R.string.AADClientId),
                getResources().getString(R.string.AADRedirectUrl),
                PromptBehavior.Auto,
                new AuthenticationCallback<AuthenticationResult>() {

                    @Override
                    public void onSuccess(final AuthenticationResult authenticationResult) {

                        if (authenticationResult != null && authenticationResult.getStatus() == AuthenticationResult.AuthenticationStatus.Succeeded) {
                            _resolver = new DependencyResolver.Builder(
                                    new OkHttpTransport().setInterceptor(new LoggingInterceptor()), new GsonSerializer(),
                                    new AuthenticationCredentials() {
                                        @Override
                                        public Credentials getCredentials() {
                                            return new OAuthCredentials(authenticationResult.getAccessToken());
                                        }
                                    }).build();

                            result.set(true);
                        }
                    }

                    @Override
                    public void onError(Exception e) {
                        result.setException(e);
                    }
                }
        );
    }

    return result;
}

public void getMessages() {
    logger.info("Getting messages...");
    Futures.addCallback(_client.getMe().getMessages().top(10).read(), new FutureCallback<List<Message>>() {
        @Override
        public void onSuccess(final List<Message> result) {
            logger.info("Preparing messages for display.");
            List<Map<String, String>> listOfMessages = new ArrayList<Map<String, String>>();

            for (Message m : result) {
                Map<String, String> oneMessage = new HashMap<String, String>();
                oneMessage.put("subject", m.getSubject());

                if (m.getFrom() != null && m.getFrom().getEmailAddress() != null) {
                    oneMessage.put("from", "From: " + m.getFrom().getEmailAddress().getAddress());
                }
                listOfMessages.add(oneMessage);
            }

            final SimpleAdapter adapter = new SimpleAdapter(MainActivity.this, listOfMessages,
                    android.R.layout.simple_list_item_2,
                    new String[]{"subject", "from"},
                    new int[]{android.R.id.text1, android.R.id.text2});

            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    lvMessages.setAdapter(adapter);
                }
            });
        }

        @Override
        public void onFailure(final Throwable t) {
            logger.error(t.getMessage(), t);
        }
    });
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    _authContext.onActivityResult(requestCode, resultCode, data);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.menu_main, menu);
    return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();

    //noinspection SimplifiableIfStatement
    if (id == R.id.action_settings) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}
Logger-Logger=LoggerFactory.getLogger(MainActivity.class);
私有静态最终字符串TAG=“MainActivity”;
私有静态最终字符串outlookBaseUrl=”https://outlook.office.com/api/v2.0";
私有身份验证上下文\u authContext;
私有依赖解析器_解析器;
私人了望客户(private OutlookClient);;
私有ListView消息;
私有字符串[]作用域=新字符串[]{”https://outlook.office.com/Mail.Read"};
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lvMessages=(ListView)findViewById(R.id.lvMessages);
Futures.addCallback(logon(),new FutureCallback()){
@凌驾
成功时的公共void(布尔结果){
_client=新的OutlookClient(outlookBaseUrl,_解析器);
getMessages();
}
@凌驾
失效时的公共无效(可丢弃的t){
logger.error(“身份验证失败”,t);
}
});
}
公共SettableFuture登录(){
最终SettableFuture结果=SettableFuture.create();
试一试{
_authContext=newauthenticationContext(this,getResources().getString(R.string.AADAuthority),true);
}捕获(例外e){
Log.e(标记“未能初始化身份验证上下文,错误为:”+e.getMessage());
_authContext=null;
结果:setException(e);
}
如果(_authContext!=null){
_authContext.acquireToken(
这
范围,
无效的
getResources().getString(R.string.AADClientId),
getResources().getString(R.string.AADRedirectUrl),
即时行为,自动,
新建AuthenticationCallback(){
@凌驾
成功时公共无效(最终身份验证结果AuthenticationResult){
if(authenticationResult!=null&&authenticationResult.getStatus()==authenticationResult.AuthenticationStatus.successed){
_resolver=新的DependencyResolver.Builder(
新的OkHttpTransport().setInterceptor(新的LoggingInterceptor()),新的GsonSerializer(),
新的AuthenticationCredentials(){
@凌驾
公共凭据getCredentials(){
返回新的OAuthCredentials(authenticationResult.getAccessToken());
}
}).build();
结果。设置(真);
}
}
@凌驾
公共无效申报人(例外e){
结果:setException(e);
}
}
);
}
返回结果;
}
public void getMessages(){
logger.info(“获取消息…”);
Futures.addCallback(_client.getMe().getMessages().top(10.read(),new FutureCallback()){
@凌驾
成功时公开作废(最终列表结果){
logger.info(“准备显示消息”);
List-listOfMessages=new-ArrayList();
for(消息m:结果){
Map oneMessage=newhashmap();
oneMessage.put(“subject”,m.getSubject());
如果(m.getFrom()!=null和&m.getFrom().getEmailAddress()!=null){
oneMessage.put(“from”,“from:”+m.getFrom().getEmailAddress().getAddress());
}
添加(一条消息);
}
最终SimpleAdapter适配器=新SimpleAdapter(MainActivity.this,listOfMessages,
android.R.layout.simple\u list\u item\u 2,
新字符串[]{“subject”,“from”},
新的int[]{android.R.id.text1,android.R.id.text2});
runOnUiThread(新的Runnable(){
@凌驾
公开募捐{
lvMessages.setAdapter(适配器);
}
});
}
@凌驾
失效时公共失效(最终可丢弃t){
logger.error(t.getMessage(),t);
}
});
}
@凌驾
受保护的void onActivityResult(int请求代码、int结果代码、意图数据){
_onActivityResult(请求代码、结果代码、数据);
}
@凌驾
公共布尔onCreateOptions菜单(菜单){
//为菜单充气;这会将项目添加到操作栏(如果存在)。
getMenuInflater().充气(右菜单菜单菜单主菜单);
返回true;
}
@凌驾
公共布尔值onOptionsItemSelected(菜单项项){
//处理操作栏项目单击此处。操作栏将
//自动处理Home/Up按钮上的点击,只要
//在AndroidManifest.xml中指定父活动时。
int id=item.getItemId();
//noinspection SimplifiableIf语句
if(id==R.id.action\u设置){
返回true;
}
返回super.onOptionsItemSelected(项目);
}
它起作用了。

但当我尝试登录时,会显示一条错误消息:

我们无法完成您的请求

Microsoft帐户遇到技术问题。请再试一次 稍后


我如何解决这个问题?

嗨,你找到解决这个问题的方法了吗?