2017-01-31 26 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之下。謝謝! –