2017-01-31 213 views
1

我在OKTA的开发人员帐户中工作。OKTA - OAuthError:redirect_uri参数非法值

我想获得一个非常简单的SPA应用程序来从OKTA获得智威汤逊。

  1. 我登录与authClient.signIn({})并且返回transaction.sessionToken/

  2. 与该会话令牌,我应该能够调用authClient.token.getWithoutPrompt({ })但我永远不能达到该代码。

我收到以下错误:OAuthError:redirect_uri参数的值非法。

如何超越此OAuth错误,以便我终于可以取回JWT。我已经尝试过OKTA GIT的例子,但无法获得任何工作。

function getJWT() 
{ 
    var orgUrl = 'https://MYXXXXXXX.oktapreview.com'; 
    var keyID = "MY CLIENT ID"; 

    var user = "MYUSER ID"; 
    var pwd = "MY PASWORD" 

    var appScope = ['openid', 'email', 'profile', 'phone', 'groups']; 

    var authClient = new OktaAuth({url: orgUrl, clientId: keyID,}); 

    $('#tokendiv').html(''); 

    authClient.signIn({ 
      username: user, 
      password: pwd, 
      redirectUri: "http://localhost:5656/test.html" //THIS IS SETUP IN MY OKTA ID 
     }) 
     .then(function(transaction) { 
      if (transaction.status === 'SUCCESS') { 
      authClient.session.setCookieAndRedirect(transaction.sessionToken); // Sets a cookie on redirect 
      console.log(transaction.sessionToken); 
      $('#tokendiv').append('<h4>Session Token</h4>' + transaction.sessionToken); 

      /// THIS IS NEVER REACHED, I Always Get OAuthError: Illegal value for redirect_uri parameter. 
      authClient.token.getWithoutPrompt({  
       responseType: 'id_token', // or array of types    
       scopes: appScope, 
       sessionToken: transaction.sessionToken 
      }) 
       .then(function(res) { 
       console.log("JWT: " + jwt); 
       $('#tokendiv').append('<h4>JWT</h4>' + res.idToken); 
       }) 
       .fail(function(err) { 
       console.log("ERROR " + err); 
       $('#tokendiv').append('<h4>Error</h4>' + err); 
       })       

      } else { 
      throw 'We cannot handle the ' + transaction.status + ' status'; 
      } 
     }) 
     .fail(function(err) { 
      console.error(err); 
     }); 
} 
+0

我发现如果我删除了authClient.token.getWithoutPrompt({}),我不再得到错误,但我需要该方法来获取JWT。我提到了 –

回答

1

只要你redirectUri包括在批准重定向的URIOkta Developer organization,你不应该接收到错误。

下面我能够成功返回在localhost:5656上运行的id_token

<!-- index.html --> 

<html> 
    <body> 
     <button onClick="getJWT()">Button</button> 
     <div id="tokendiv"></div> 
    </body> 
</html> 

<script> 
    function getJWT(){ 
     var orgUrl = 'https://{{org}}.oktapreview.com'; 
     var keyID = "{{clientId}}"; 

     var user = "{{user}}"; 
     var pwd = "{{password}}" 

     var appScope = ['openid', 'email', 'profile', 'phone', 'groups']; 

     var authClient = new OktaAuth(
      { 
       url: orgUrl, 
       clientId: keyID, 
       redirectUri: "http://localhost:5656/index.html" 
      }); 

     $('#tokendiv').html(''); 

     authClient.signIn({ 
       username: user, 
       password: pwd, 
     }) 
     .then(function(transaction) { 
      if (transaction.status === 'SUCCESS') { 
       authClient.token.getWithoutPrompt({  
        responseType: 'id_token', // or array of types    
        scopes: appScope, 
        sessionToken: transaction.sessionToken 
       }) 
       .then(function(res) { 
        $('#tokendiv').append('<h4>JWT</h4>' + res.idToken); 
        console.log("JWT: " + JSON.stringify(res)); 
       }) 
       .fail(function(err) { /* err */ })       
      } else { /* throw error */ } 
     }) 
     .fail(function(err) { /* err */ }); 
    } 
</script> 
+0

。 redirectUri:“http:// localhost:5656/test.html”//这是设置在我的OKTA –

+1

只是想验证。 OktaAuth发送当前浏览器url,而不是配置对象中指定的redirectUri。我已经更新了我的答案,以显示如何设置重定向。 – jmelberg

+0

您的更新告诉我这个问题。我在authClient.signIn下有redirectUri,但是(如你所示)它应该在var authClient = new OktaAuth之下。谢谢! –