kimpro82 / MyBizApps

Business? ??
MIT License
1 stars 0 forks source link

Web / TypeScript : GitHub REST API 1.1 - Make a Request to the GitHub API using Token Authentication #50

Open kimpro82 opened 10 months ago

kimpro82 commented 10 months ago

Terrible

{
  "message": "API rate limit exceeded for ***.***.***.***. (But here's the good news: Authenticated requests get a higher rate limit. Check out the documentation for more details.)",
  "documentation_url": "https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting"
 }

https://docs.github.com/rest/overview/resources-in-the-rest-api#rate-limiting

kimpro82 commented 10 months ago

Actually now I'm not certain why I am doing this task with TypeScript, not Python, anyway ……

by ChatGPT

Here's how you can handle token management using environment variables in TypeScript:

  1. Install the dotenv Package: Begin by installing the dotenv package to manage environment variables and load them from a .env file.

    npm install dotenv
  2. Create a .env File: Create a .env file at the root of your project and store your token securely within it.

    // Contents of the .env file
    GITHUB_TOKEN=YOUR_AUTH_TOKEN
  3. Utilize Environment Variables in TypeScript Code: Next, import dotenv in your TypeScript code to load the environment variables from the .env file.

    import * as dotenv from 'dotenv';
    import axios, { AxiosRequestConfig } from 'axios';
    
    // Load the .env file
    dotenv.config();
    
    // Assign the token variable by accessing the environment variable
    const token = process.env.GITHUB_TOKEN;
    
    if (!token) {
      throw new Error('Token not found.');
    }
    
    // Configure for API requests
    const config: AxiosRequestConfig = {
      headers: {
        Authorization: `token ${token}`
      }
    };
    
    // Set up the GitHub API endpoint
    const apiUrl = 'https://api.github.com/repos/kimpro82/MyPractice/languages';
    
    // Send a GET request using Axios
    axios.get(apiUrl, config)
      .then(response => {
        console.log(response.data); // API response data
      })
      .catch(error => {
        console.error('API request failed:', error);
      });

By calling dotenv.config(), the .env file gets loaded, and process.env.GITHUB_TOKEN retrieves the GITHUB_TOKEN environment variable. This method allows you to manage your token securely in an external file, enhancing security by preventing direct exposure within your source code.

kimpro82 commented 10 months ago

Hmm the url https://api.github.com/repos/kimpro82/MyPractice/languages and GetGithubUserStats.ts work again??