离子:由于ERR_connection_refused,无法加载http:// localhost / callback?code =

时间:2018-08-18 12:02:17

标签: ionic-framework oauth-2.0 cordova-plugins linkedin-api

我正在尝试使用rest-api方法在ionic中实现具有linkedin功能的登录。

linkedinLogin() {
    this.platform.ready().then(() => {
      this.linkedinPage().then(success => {
        alert(success.access_token);
      },(error) => {
        alert(error);
      });
    });
  }

  linkedinPage(): Promise<any> {
    var self = this;
    return new Promise((resolve, reject) => {
      var browserRef = window.cordova.InAppBrowser.open('https://www.linkedin.com/uas/oauth2/authorization?client_id=' + this.clientId + '&redirect_uri=' + this.redirect_uri + '&scope=' + this.appScope + '&response_type=code&state=' + this.state, '_blank', 'location=no,clearsessioncache=yes,clearcache=yes');
      browserRef.addEventListener("loadstart", (event) => {
        if((event.url).indexOf(this.redirect_uri) === 0) {
          try {
            var requestToken = (event.url).split("code=")[1].split("&")[0];
            let headers = new Headers();
            headers.append('Content-Type', 'application/x-www-form-urlencoded');
            this.http.post("https://www.linkedin.com/oauth/v2/accessToken?client_id='xxxx'&client_secret='xxxxxx'&redirect_uri=http://localhost/callback&grant_type=authorization_code&code=" + requestToken,
            {headers: headers})
            .subscribe(function(data){
              resolve(data);
              alert(data);
            })

          } catch(e) {
              setTimeout(function() {
                browserRef.close();
              }, 10);
            }
        }
        else {
          browserRef.addEventListener("exit", function(event) {
            reject("The linkedin sign in flow was canceled");
        });
        }
      });
  });
  }

我能够重定向到linkedin页面,并输入我的凭据,以允许linkedin访问权限。在浏览器中收到错误消息后, error message

有人可以告诉我为什么我遇到此错误以及如何解决该错误吗?

1 个答案:

答案 0 :(得分:0)

这是因为因为CROS,linkedin永远都不允许您访问其API表单localhost。您是否尝试在真实设备中运行应用程序?如果没有在实际设备上尝试并查看控制台,则可以从那里找到准确的错误。

相关问题