Passwordless sftp using a different user than the runtime user

I am running a shell script as user A. In that script I need to execute a sftp that would transfer the file using another user B.
I am using the below command:

sftp -oPort22 B@remote server

However, I am getting password prompts each time.
I have done the following:

  1. Added the public contents from local server to remoter servers /home/<user>/.ssh/authorized_keys.
  2. Tried changing permission of .ssh directory to both 700 and 744. Also, tried changing permission of authorized_keys to both 600 and 644.

Nothing has worked so far.

I guess, the command I am passing is correct as sftp is happening; but the passwordless authorization not working may be due to some file permissions issues.
Need your inputs

What user's authorized_keys did you add the key to? Needs to be user B 's!

So, I have added content of public key or authorized_keys of user B to user A's. I am not sure what file permission should be for both

The public key created by user A (and matching his or her private key) needs to be in user B's remote authorized_keys file.

Thanks a lot RuDic...it worked...:):):):):b::b::b::b::b: