0

Instead of keeping keys in my application I intent to read the keys from local file system into a variable (array of strings) and use those array elements in my oAuth APIs. However, when i used keys (in plaintext) as argument to OAuth APIs, authentication succeeds. BUT authentication failed when same value in read into a variable from file & that variable is passed to OAuth API. Tried comparing the key value and variable value t find out they don't match though they same exactly same.

Input file looks as below:

$cat .keys
k1='jFOMZ0bI60fDAEKw53lYCj2r4'
k2='LNkyPehneIi8HeqTg1ji74H42jFkkBxZolRfzNFmaJKwLg7R7E'


secret_keys=[]

def keys_io():
    key_file = open('/Users/homie/.keys', 'r+')
    for key in range(1,5):
        secret_keys.append(key_file.readline().split("=")[1])

    print secret_keys[0]
    print (secret_keys[0] == "jFOMZ0bI60fDAEKw53lYCj2r4")

keys_io()       

Output:

jFOMZ0bI60fDAEKw53lYCj2r4
False

What am i missing here?

5
  • 1
    The key you read from the file still has a leading and a trailing ' (single quotation mark) Commented Jul 28, 2016 at 6:05
  • Removed leading & terminal single quotation marks. still same result Commented Jul 28, 2016 at 6:10
  • Show the output of print repr(secret_keys[0]). Commented Jul 28, 2016 at 6:18
  • No trailing \n when you read from the file? What does comparing lengths show? Commented Jul 28, 2016 at 6:19
  • Yes there us \n at the trailing end of secret_keys[0] Commented Jul 28, 2016 at 6:25

3 Answers 3

1

You should strip the key that you read from the file, as it has a trailing \n:

print(secret_keys[0].strip() == "jFOMZ0bI60fDAEKw53lYCj2r4")

Or do it when reading it:

for key in range(1,5):
    secret_keys.append(key_file.readline().split("=")[1].strip())
Sign up to request clarification or add additional context in comments.

Comments

1

If leading-trailing characters are bugging you, remove them with slicing, i.e [1:-1] to remove first-last quotations.

I also refactored your function a bit:

def keys_io():
    with open('.keys', 'r+') as f:
        for line in f:
            secret_keys.append(line.split('=')[1].strip()[1:-1])

    print secret_keys[0]
    print (secret_keys[0] == "jFOMZ0bI60fDAEKw53lYCj2r4"
  • Use a context manager to open/close your file automatically.
  • Use for line in <opened_file> instead of other methods if you need to examine all lines.
  • Use strip() without arguments to remove unwanted space.

After these changes, the keys_io file works like a charm for me when using the .key file you presented.

1 Comment

"Use strip() without arguments to remove unwanted space." And new line chars :)
0

When you read a text file from Python, you need to escape the new line character first. Another problem is you have single quote in between the input text. So you need to make change to:

secret_keys.append(key_file.readline().strip().split("=")[1])

and

if(secret_keys[0] == "\'jFOMZ0bI60fDAEKw53lYCj2r4\'"):

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.