Yammer授权错误

时间:2014-08-13 12:43:09

标签: jquery ajax http-headers jsonp yammer

尝试运行此代码时出现以下错误:

        function getCurrentUser() {
        yam.platform.request({
            // yam.request({
            url: "users/current.json", //this is one of many REST endpoints that are available
            method: "GET",

            data: {},
            success: function (user) { //print message response information to the console
                console.log("User request was successful.");
                console.dir(user);
                toggleLoginStatus(true);
                $('#authResult').html('User Result:<br/>');
                for (var field in user) {
                    $('#authResult').append(' ' + field + ': ' +
                        escape(user[field]) + '<br/>');
                }

            },
            error: function (user) {
                console.error("There was an error with the request.");
            }
        });

    }  

Error: XMLHttpRequest cannot load https://api.yammer.com/api/v1/users/current.json?_=1407933095976. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'https://brillioonline.sharepoint.com' is therefore not allowed access.

我正在尝试使用以下代码来解决错误,但不知道这里有什么令牌:

$.ajax(
'https://app.asana.com/api/1.0/users/me',
{
type: 'GET',
dataType: 'json',
beforeSend: function (xhr) {
  xhr.setRequestHeader("Authorization", "Bearer $token")
},
complete: function (resp) {
  console.log(resp);
},
error: function (jqXHR,  textStatus,  errorThrown) {
  console.log(textStatus);
}
}
 );

请帮助我,我是初学者

1 个答案:

答案 0 :(得分:1)

首先,您不需要使用Ajax或手动设置标题。 Yammer js sdk为你做的。但是如果你真的想使用Ajax,你可以从getLoginStatus的响应中获取令牌(见下文)

要获得没有Ajax的当前用户,请确保您已登录。以下是一个示例:

function login() {
    yam.platform.getLoginStatus(function (response) {
          if (response.authResponse) { //if already logged in
              alert("token=" + response.access_token.token);
          } else {
              yam.platform.login(function (response) {
                  if (response.authResponse) {
                      alert("token=" + response.access_token.token);
                  }
              });
          }
    });
}

2。成功登录后,运行您的请求。如果仍然无效,请在getLoginStatus块内运行请求:

yam.platform.getLoginStatus(function (response) {
      if (response.authResponse) {
          //RUN YOUR REQUEST HERE.
      } else {
          alert("something went wrong");
      }
});