Advertisement
Guest User

Untitled

a guest
Jul 9th, 2018
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.21 KB | None | 0 0
  1. <?php
  2.  
  3. //conexão sql
  4. $servername = "xxx";
  5. $database = "xxx";
  6. $username = "xxx";
  7. $password = "xxx";
  8. //conectando
  9. $conn = mysqli_connect($servername, $username, $password, $database);
  10. //checando conexão
  11. if (!$conn) {
  12. die("Falha na conexão: " . mysqli_connect_error());
  13. }
  14.  
  15. echo "Conectado com sucesso!";
  16.  
  17. require __DIR__ . '/vendor/autoload.php';
  18.  
  19. session_start();
  20.  
  21. echo ('Main screen turn on!<br/><br/>');
  22.  
  23. $provider = new \Wohali\OAuth2\Client\Provider\Discord([
  24. 'clientId' => 'xxx',
  25. 'clientSecret' => 'xxx',
  26. 'redirectUri' => 'xxx'
  27. ]);
  28.  
  29. if (!isset($_GET['code'])) {
  30.  
  31. // Step 1. Get authorization code
  32. $authUrl = $provider->getAuthorizationUrl();
  33. $_SESSION['oauth2state'] = $provider->getState();
  34. header('Location: ' . $authUrl);
  35.  
  36. // Check given state against previously stored one to mitigate CSRF attack
  37. } elseif (empty($_GET['state']) || ($_GET['state'] !== $_SESSION['oauth2state'])) {
  38.  
  39. unset($_SESSION['oauth2state']);
  40. exit('Invalid state');
  41.  
  42. } else {
  43.  
  44. // Step 2. Get an access token using the provided authorization code
  45. $token = $provider->getAccessToken('authorization_code', [
  46. 'code' => $_GET['code']
  47. ]);
  48.  
  49. // Show some token details
  50. echo '<h2>Token details:</h2>';
  51. echo 'Token: ' . $token->getToken() . "<br/>";
  52. echo 'Refresh token: ' . $token->getRefreshToken() . "<br/>";
  53. echo 'Expires: ' . $token->getExpires() . " - ";
  54. echo ($token->hasExpired() ? 'expired' : 'not expired') . "<br/>";
  55.  
  56. // Step 3. (Optional) Look up the user's profile with the provided token
  57. try {
  58.  
  59. $user = $provider->getResourceOwner($token);
  60.  
  61. // echo '<h2>Resource owner details:</h2>';
  62. // printf('Hello %s#%s!<br/><br/>', $user->getUsername(), $user->getDiscriminator());
  63.  
  64.  
  65. $dados = $user->toArray();
  66. $idd = $dados['id'];
  67. echo '<br>seu id é: ' . $dados['id'];
  68. $pegardados = "SELECT id FROM get_discord WHERE id = .$dados['id']";
  69. if (mysqli_query($conn, $pegardados)) {
  70. echo "dadospegos";}
  71. if($pegardados == $idd)
  72. {echo = "redirecionando";}
  73. else
  74. {echo = "cadastrando";}
  75. {echo = "redirecionando";}
  76. else {
  77. echo "Error: " . $sql . "<br>" . mysqli_error($conn);
  78. }
  79.  
  80. } catch (Exception $e) {
  81.  
  82. // Failed to get user details
  83. exit('Oh dear...');
  84.  
  85. }
  86. }
  87. // create $provider as in the initial example
  88. $existingAccessToken = getAccessTokenFromYourDataStore();
  89.  
  90. if ($existingAccessToken->hasExpired()) {
  91. $newAccessToken = $provider->getAccessToken('refresh_token', [
  92. 'refresh_token' => $existingAccessToken->getRefreshToken()
  93. ]);
  94.  
  95. // Purge old access token and store new access token to your data store.
  96. }
  97. // create $provider as in the initial example
  98. try {
  99.  
  100. // Try to get an access token using the client credentials grant.
  101. $accessToken = $provider->getAccessToken('client_credentials');
  102.  
  103. } catch (\League\OAuth2\Client\Provider\Exception\IdentityProviderException $e) {
  104.  
  105. // Failed to get the access token
  106. exit($e->getMessage());
  107.  
  108. }
  109.  
  110. mysqli_close($conn);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement