LINE应用程序登录api auth2无法正常工作

时间:2016-04-27 09:00:07

标签: cordova web-applications oauth-2.0 line

我正在开发phonegap / cordova crossplatform应用程序。我想通过LINE帐户登录。

我已按照https://developers.line.me/web-login/integrating-web-login中的说明进行操作,这意味着我已经 channel_id,client_secret ,设置 callback_url http://localhost

我使用以下链接从LINE应用程序打开登录页面

https://access.line.me/dialog/oauth/weblogin?response_type=code&client_id= CHANNEL_ID &安培; REDIRECT_URI = http://localhost&state=

按下LOGIN按钮后,它回拨到https://access.line.me/dialog/oauth/authorize,文字说:“抱歉,登录时遇到问题。请稍等一会儿再试一次。” - 我等了再试一次,但它永远不会奏效。

var lineapi = {
      authorize: function(options) {
          var deferred = $.Deferred();

          //Build the OAuth consent page URL
          //https://access.line.me/dialog/oauth/weblogin?response_type=code&client_id={Channel ID}&redirect_uri={Redirect URL}&state={State}
          var authUrl = 'https://access.line.me/dialog/oauth/weblogin?' + $.param({
              client_id: options.channel_id,
              redirect_uri: options.redirect_uri,
              response_type: 'code',
              // scope: options.scope,
              state: ''
          });

          //Open the OAuth consent page in the InAppBrowser
          var authWindow = window.open(authUrl, '_blank', 'location=no,toolbar=no');

          //The recommendation is to use the redirect_uri "urn:ietf:wg:oauth:2.0:oob"
          //which sets the authorization code in the browser's title. However, we can't
          //access the title of the InAppBrowser.
          //
          //Instead, we pass a bogus redirect_uri of "http://localhost", which means the
          //authorization code will get set in the url. We can access the url in the
          //loadstart and loadstop events. So if we bind the loadstart event, we can
          //find the authorization code and close the InAppBrowser after the user
          //has granted us access to their data.
          $(authWindow).on('loadstart', function(e) {
              console.log('loadstart:',e);
              var url = e.originalEvent.url;
              console.log("url:",url);
              var code = /\?code=(.+)$/.exec(url);
              var error = /\?error=(.+)$/.exec(url);
              var callback = /\?callback=(.+)$/.exec(url);

              if (code || error) {
                  //Always close the browser when match is found
                  authWindow.close();
              }

              if (code) {
                  //Exchange the authorization code for an access token
                  console.log('calling oauth for accessToken');
                  $.post('https://api.line.me/v1/oauth/accessToken', {
                      grant_type: 'authorization_code',
                      client_id: options.channel_id,
                      client_secret: options.channel_secret,
                      code: code,
                      redirect_uri: options.redirect_uri
                  }).done(function(data) {
                      alert(data);
                      deferred.resolve(data);
                  }).fail(function(response) {
                      alert(response);
                      deferred.reject(response.responseJSON);
                  });
              } else if (error) {
                  //The user denied access to the app
                  console.log("error:",error);
                  deferred.reject({
                      error: error[1]
                  });
              }
          });

          return deferred.promise();
      }
  };

  $scope.loginLine = function(){

    lineapi.authorize({
        channel_id: ***my_channel_id***,
        channel_secret: ***my_channel_secret***,
        redirect_uri: 'http://localhost',
        scope: 'PROFILE'
    });
  }

有谁知道如何解决这个问题? 非常感谢您的帮助。

1 个答案:

答案 0 :(得分:0)

在行登录表单中输入电子邮件/密码凭据后,请查看地址行中的结果URL - 这应该包含如下错误消息:

https://access.line.me/dialog/oauth/weblogin?response_type=code&forceSecondVerification=false&userId=XXX%40XXXXXX.com&client_id=XXXXXXXXX&showPermissionApproval=&displayType=b&idProvider=1&redirect_uri=http%3A%2F%2Flocalhost%3A4000%2Fauth%2Fline%2Fcallback&state=&lang=&errorCode=400&errorMessage=AUTH_INVALID_REDIRECT_URL

在我的示例中,我有无效的重定向网址和错误消息errorMessage=AUTH_INVALID_REDIRECT_URL

由于您的oauth网址包含localhost,我认为您的errorMessage将是相同的。

文档here说:

  

重定向网址必须与注册频道时指定的其中一个网址相匹配。比较目标是从方案到URL的路径。查询参数不包括在比较目标中。指定URL编码的字符串以指定查询参数值。