OKTA - OAuthError:redirect_uri参数

时间:2017-01-31 21:44:52

标签: oauth2 okta okta-api

我在OKTA的开发者帐户工作。

我正在尝试使用非常简单的SPA应用程序从OKTA获得JWT。

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

  2. 使用该会话令牌,我应该可以调用 authClient.token.getWithoutPrompt({}),但我永远无法访问该代码。

  3. 我收到以下错误: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);
            });
    }
    

1 个答案:

答案 0 :(得分:2)

只要您的redirectUri包含在Okta Developer organization中已批准的重定向URI 中,就不应该收到该错误。

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

<!-- 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>