簡體   English   中英

snapchat 登錄套件 web:授權無效,驗證碼無效

[英]snapchat login kit web: invalid grant, invalid code verifier

我在我的 PHP 項目中使用 Snapchat 登錄套件 web。 我成功連接了用戶授權頁面。 授予授權后,我將在我的 redirect_uri 頁面中獲取代碼和 state GET 變量。 我需要一個訪問令牌,但是當我繼續下一步時,我收到一個錯誤響應,

1.invalid_grant 2.invalid code_verifier

這是我的登錄頁面和重定向頁面代碼:

--登錄頁面---

<?php

if(isset($_POST['login']))
{

$url="https://accounts.snapchat.com/accounts/oauth2/auth";
$clientId="my_client_id_get_from_snapchat_app_setting";
$client_secret="my_client_secrect_get_from_snapchat_app_setting";
$redirectUri="https://Snapreport.org/Redirect.php";

$method= "GET";

$str = 'arifusingsnapchat'; 
  
$state= base64_encode($str);

 $code_verifier = "arifusingsnapchat225678909fghh8df777634567890";
 $code_verifier_hash = hash("sha256",$code_verifier);

 $code_challenge = base64_encode($code_verifier_hash);



$scopeList= array("https://auth.snapchat.com/oauth2/api/user.display_name",
                   "https://auth.snapchat.com/oauth2/api/user.bitmoji.avatar",
                   "https://auth.snapchat.com/oauth2/api/user.external_id"
);

$scope = implode($scopeList," ");

$stringArr = array(
    "client_id" => $clientId,
    "client_secret" => $client_secret,
    "redirect_uri" => $redirectUri,
    "code_challenge" => $code_challenge,
    "code_challenge_method"=> "S256",
    "response_type" => "code",
    "scope" => $scope,
    "state" => $state );

$query= http_build_query($stringArr, '', '&'); 

$request = $url."?".$query;

header("Location:".$request);

}
 ?>

--redirect_uri 頁面--

<?php

if(isset($_GET['code']) && isset($_GET['state']))
{ 
  $code= $_GET['code'];
  $state=$_GET['state'];

  
  $url="https://accounts.snapchat.com/accounts/oauth2/token";
  $clientId="my_client_id_get_from_snapchat_app_setting";
  $client_secret="my_client_secrect_get_from_snapchat_app_setting";
  $redirect_uri="https://Snapreport.org/Redirect.php";

  $header = base64_encode($clientId.":".$client_secret);
 
  $code_verifier = "arifusingsnapchat225678909fghh8df777634567890";
  
  $payloaded_url=$url."?client_id=".$clientId."&client_secret=".$client_secret."&grant_type=authorization_code&redirect_uri=".$redirect_uri."&code=".$code."&code_verifier=".$code_verifier; 

  $ch = curl_init($payloaded_url);
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_POST, 1);

  curl_setopt($ch, CURLOPT_HTTPHEADER, array(
      'Content-Type' => 'application/json',
       'Authorization'=> 'Basic '.$header
  ));
  // execute!
  $response = curl_exec($ch);

  // close the connection, release resources used
  curl_close($ch);
  
 $res= json_decode($response);
  // do anything you want with your response
 echo "<pre>";
  var_dump($res);

  echo "</pre>";
} 

Snapchat 登錄工具包 Web 文檔Snapchat 登錄工具包 Web 文檔https://kit.snapchat.com/docs/login-kit-web

在您的登錄頁面上:

$code_verifier_hash = urlencode(pack('H*', hash('sha256', $code_verifier)))

您可能還應該使用 B64 安全 url 編碼器,如下所示:

https://github.com/F21/jwt/blob/master/JWT/JWT.php#L120

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM