【问题标题】:Yammer authorization errorYammer 授权错误
【发布时间】:2014-08-13 12:43:09
【问题描述】:

我在尝试运行此代码时收到以下错误:

        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);
}
}
 );

请帮帮我,我是初学者

【问题讨论】:

    标签: jquery ajax http-headers jsonp yammer


    【解决方案1】:

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

    要在没有 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");
          }
    });
    

    【讨论】:

    • 您好,感谢您的建议。我已经尝试了上面的代码,但是尽管日志记录处于活动状态,但我仍然收到第 1 步的访问被拒绝错误。你能帮我解决这个问题吗?
    • 第 1 步的第 2 行未定义响应。目前我正在加载以下 js-platform_embed.js 和 platform_js_sdk.js。是否需要加载任何其他引用。
    • platform_js_sdk.js 就足够了。那么您是否将您的域添加到 yammer 上的 JavaScript 源白名单?它位于:yammer.com/client_applications 并转到您的应用 --> 基本信息 --> 安装信息 --> Javascript 起源
    • 我们在哪里添加域?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-04-24
    • 2018-06-30
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多