outlook邮件REST API

时间:2017-04-08 13:09:31

标签: 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 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);
}

有效。 Image

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

  

我们无法完成您的请求

     

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

Image

我该如何解决这个问题?

0 个答案:

没有答案