Advertisement
Guest User

Untitled

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