【问题标题】:Decoding the information from token with janrain使用 janrain 从令牌中解码信息
【发布时间】:2025-12-19 16:35:12
【问题描述】:

我在我的网站上使用 janrain 小部件进行 OAuth 流程。我正在以下 URL 中对此进行测试:http://vignesh.gvignesh.org/register/

单击注册按钮后,用户从 Google 或 Yahoo 或 Facebook 登录。我能够获取令牌并将其存储在变量中。我在我的站点中显示令牌以进行测试。现在我不知道如何从我得到的令牌中提取用户信息。

【问题讨论】:

    标签: oauth janrain


    【解决方案1】:
    <?php
    
    $rpxApiKey = 'YOUR_API_KEY_HERE';
    
    if(isset($_POST['token'])) {
    
      /* STEP 1: Extract token POST parameter */
      $token = $_POST['token'];
    
      /* STEP 2: Use the token to make the auth_info API call */
      $post_data = array('token' => $_POST['token'],
                         'apiKey' => $rpxApiKey,
                         'format' => 'json');
    
      $curl = curl_init();
      curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
      curl_setopt($curl, CURLOPT_URL, 'https://rpxnow.com/api/v2/auth_info');
      curl_setopt($curl, CURLOPT_POST, true);
      curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
      curl_setopt($curl, CURLOPT_HEADER, false);
      curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
      $raw_json = curl_exec($curl);
      curl_close($curl);
    
    
      /* STEP 3: Parse the JSON auth_info response */
      $auth_info = json_decode($raw_json, true);
    
      if ($auth_info['stat'] == 'ok') {
    
        /* STEP 3 Continued: Extract the 'identifier' from the response */
        $profile = $auth_info['profile'];
        $identifier = $profile['identifier'];
    
        if (isset($profile['photo'])) {
          $photo_url = $profile['photo'];
        }
    
        if (isset($profile['displayName'])) {
          $name = $profile['displayName'];
        }
    
        if (isset($profile['email'])) {
          $email = $profile['email'];
        }
    
        print $identifier;
            echo "</br>";
        print $photo_url;
            echo "</br>";
        print $name;
            echo "</br>";
        print $email;
        /* STEP 4: Use the identifier as the unique key to sign the user into your system.
    This will depend on your website implementation, and you should add your own
    code here.
    */
    
    /* an error occurred */
    } else {
      // gracefully handle the error. Hook this into your native error handling system.
      echo 'An error occured: ' . $auth_info['err']['msg'];
    }
    }
    ?>
    

    这将从我们在身份验证后获得的令牌中提取信息。

    【讨论】: