使用Microsoft-Graph-Client获取日历事件

时间:2018-08-24 08:37:53

标签: azure vue.js

我设法通过oauth2 API获取了access_token,

但是当我尝试检索对事件的访问时,出现以下错误:

状态码:400,代码:“ BadRequest”,消息:“当前认证的上下文对于单页Web应用程序的OAuth 2.0隐式流无效。”

我创建了一个Vue组件,这是返回令牌的函数:

getToken: function() {
  var self = this;
  var params = new URLSearchParams();
  params.append('client_id', self.client_id);
  params.append('client_secret', self.client_secret);
  params.append('grant_type', self.tokenSettings.grant_type);
  params.append('resource', self.tokenSettings.resource);
  var url = self.tokenSettings.url;
  axios.post(url,
      params, {
        withCredentials: false
      })
    .then(function(response) {
      self.access_token = response.data.access_token;
      self.token_type = response.data.token_type;
      self.expires_in = response.data.expires_in * 1000;
      self.isAuthenticated = true;
      self.getEvents();
      window.setInterval(function() {
        self.getToken();
      }.bind(self), response.data.expires_in * 1000);
    })
    .catch(function(error) {
      self.isAuthenticated = false;
    });

}

下面是我尝试获取事件的函数,但返回上面的错误:

getEvents: function() {
  var self = this;
  var apiUrl = '/me/events';
  if (self.access_token) {
    // Create a Graph client
    var client = MicrosoftGraph.Client.init({
      authProvider: (done) => {
        // Just return the token
        done(null, self.access_token);
      }
    });
    client
      .api(apiUrl)
      .select('*')
      .orderby('createdDateTime DESC')
      .get((err, res) => {
        console.log('res', res);
        if (err) {
          console.log('error:', err);
          self.isAuthenticated = false;
        } else {
          self.events = res.value;
        }
      });
  } else {
    self.isAuthenticated = false;
  }
}

0 个答案:

没有答案