我从Python2.7代码调用的API有效,但从Python3无法工作。...获取“无效的ID /秘密密钥组合” hmac.new

时间:2019-04-20 01:43:23

标签: python-3.x hmac

我正在尝试从有效的Python2.7代码进行API调用,但无法从python3.6进行API调用。...我的ID /秘密组合未正确生成,并且返回了403响应并指出ID / secret在Python3.6上不正确。我尝试了Python3.6中的所有可能组合,但无济于事...

下面是python2.7和3.6的代码

  ---------Python2.7---------
def generate_auth_string(self, method):
        authstring = "%s\n%s/%s/%s\n%s\n%s\n" % (
            self.formatted_date,
            self.service_url.rstrip('/'),  # this is merged with resource and "method" below
            self.resource.strip('/'),   # merged
            method.strip('/'),   # call scope (not http verb)
            self.content_type,
            self.method   # HTTP VERB
        )
        hash1 = hmac.new(self.secret, authstring, hashlib.sha1).digest()
        return "MPA %s:%s" % (self.key_id, base64.b64encode(hash1))

---------Python3.6---------
    def generate_auth_string(self, method):
        authstring = "%s\n%s/%s/%s\n%s\n%s\n" % (
            self.formatted_date,
            self.service_url.rstrip('/'),  # this is merged with resource and "method" below
            self.resource.strip('/'),   # merged
            method.strip('/'),   # call scope (not http verb)
            self.content_type,
            self.method   # HTTP VERB
        )
        secret = bytes(self.secret, 'utf-8')
        authstring = bytes(authstring, 'utf-8')
        return "MPA %s:%s" % (self.key_id, base64.b64encode(hmac.new(secret, authstring, hashlib.sha1).digest()).decode("utf-8"))

0 个答案:

没有答案