我目前正在尝试编写一个脚本来发送请求令牌,我有标题和声明集,但我不明白签名! OAuth要求使用SHA256withRSA(也称为带有SHA-256哈希函数的RSASSA-PKCS1-V1_5-SIGN)加密我的私钥,但我能找到的最接近的是RSAES-PKCS1-v1_5(具有RSA和SHA- 256哈希)。我按照这个例子,并调整它,所以我可以设置它,但继承我的dillema: 签名=“” h = SHA.new(签名)
key = RSA.importKey(open('C:\Users\Documents\Library\KEY\My Project 905320c6324f.json').read())
cipher = PKCS1_v1_5.new(key)
ciphertext = cipher.encrypt(message+h.digest())
print(ciphertext)
我有点迷失,我给的JSON文件有公钥和私有,我是否将私钥复制并粘贴到签名变量中(它给了我一个无效的语法)?或者我是否再次通过该目录?我迷失了,而且我的头脑哈哈。我目前正在运行Python 3.4,使用pyCrypto作为签名。
答案 0 :(得分:0)
根据您在下面所说的关于想要使用gmail编写命令系统的内容,我编写了一个使用IMAP执行此操作的简单脚本。我认为这可能比尝试为单个用户使用Google API更简单,除非您只是想练习。
import imaplib, logging
from time import sleep
USERNAME = 'YOUR_USERNAME_HERE' # For gmail, this is your full email address.
PASSWORD = 'YOUR_PASSWORD_HERE'
CHECK_DELAY = 60 # In seconds
LOGGING_FORMAT = '%(asctime)s %(message)s'
logging.basicConfig(filename='imapTest.log', format=LOGGING_FORMAT, level=logging.INFO)
logging.info("Connecting to IMAP server...")
imap = imaplib.IMAP4_SSL('imap.gmail.com')
imap.login(USERNAME, PASSWORD)
logging.info("Connected to IMAP server.")
def get_command_messages():
logging.info("Checking for new commands.")
imap.check()
# Search the inbox (server-side) for messages containing the subject 'COMMAND' and which are from you.
# Substitute USERNAME below for the sending email address if it differs.
typ, data = imap.search(None, '(FROM "%s" SUBJECT "COMMAND")' %(USERNAME))
return data[0]
def delete_messages(message_nums):
logging.info("Deleting old commands.")
for message in message_nums.split():
imap.store(message, '+FLAGS', '\\DELETED')
imap.expunge()
# Select the inbox
imap.select()
# Delete any messages left over that match commands, so we are starting 'clean'.
# This probably isn't the nicest way to do this, but saves checking the DATE header.
message_nums = get_command_messages()
delete_messages(message_nums)
try:
while True:
sleep(CHECK_DELAY)
# Get the message body and sent time. Use BODY.PEEK instead of BODY if you don't want to mark the message as read, but we're deleting it anyway below.
message_nums = get_command_messages()
if message_nums:
# search returns space-separated message IDs, but we need them comma-separated for fetch.
typ, messages = imap.fetch(message_nums.replace(' ', ','), '(BODY[TEXT])')
logging.info("Found %d commands" %(len(messages[0])))
for message in messages[0]:
# You now have the message body in the message variable.
# From here, you can check against it to perform commands, e.g:
if 'shutdown' in message:
print("I got a shutdown command!")
# Do stuff
delete_messages(message_nums)
finally:
try:
imap.close()
except:
pass
imap.logout()
如果您已开始使用Gmail API,Google强烈建议您使用现有的Python库,而不是尝试自己进行完全身份验证等。有了它,它应该 - 或多或少 - 是用相关的Gmail API替换上面的imap调用的情况。