Home > Blockchain >  Where would i put my API Keys in this script?
Where would i put my API Keys in this script?

Time:11-03

I am new to coding in general. But have never used PHP before. It is not very clear on where to put my keys.

More context: https://github.com/erikaheidi/dynacover

`

<?php

return [
    //Twitter API Keys
    'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY',
    'twitter_consumer_secret' => getenv('TW_CONSUMER_SECRET') ?: 'APP_CONSUMER_SECRET',
    'twitter_user_token' => getenv('TW_USER_TOKEN') ?: 'USER_ACCESS_TOKEN',
    'twitter_token_secret' => getenv('TW_USER_TOKEN_SECRET') ?: 'USER_ACCESS_TOKEN_SECRET',

    //GitHub Personal Token (for templates using GH Sponsors)
    'github_api_bearer' => getenv('GITHUB_TOKEN') ?: 'GITHUB_API_BEARER_TOKEN',

    //Default Template
    '`default_template`' => getenv('DEFAULT_TEMPLATE') ?: 'app/Resources/templates/cover_basic.json'
];

`

CodePudding user response:

Let's take the following line of code to analyse where the credentials are stored

'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY'
  • PHP will first use getenv() to obtain the environment variable TW_CONSUMER_KEY
  • If the environment variable could not be found, it uses the constant string "APP_CONSUMER_KEY" - this probably isn't very helpful in your case

Having established the credentials are designed to be stored in the environment variables in your sample, maybe this answer will help you on the next step: How to set global environment variables for PHP

For now, you could set the variables using the following code to test if the credentials work:

putenv('TW_CONSUMER_KEY=your_key_here');
putenv('TW_CONSUMER_SECRET=your_secret_key');
// And so on, and so forth :)

return [
    //Twitter API Keys
    'twitter_consumer_key' => getenv('TW_CONSUMER_KEY') ?: 'APP_CONSUMER_KEY',
    'twitter_consumer_secret' => getenv('TW_CONSUMER_SECRET') ?: 'APP_CONSUMER_SECRET',
    'twitter_user_token' => getenv('TW_USER_TOKEN') ?: 'USER_ACCESS_TOKEN',
    'twitter_token_secret' => getenv('TW_USER_TOKEN_SECRET') ?: 'USER_ACCESS_TOKEN_SECRET',

    //GitHub Personal Token (for templates using GH Sponsors)
    'github_api_bearer' => getenv('GITHUB_TOKEN') ?: 'GITHUB_API_BEARER_TOKEN',

    //Default Template
    '`default_template`' => getenv('DEFAULT_TEMPLATE') ?: 'app/Resources/templates/cover_basic.json'
];
  •  Tags:  
  • php
  • Related