fzlee / alipay

Python Alipay(支付宝) SDK with SHA1/SHA256 support
Other
1.29k stars 378 forks source link

ISVAliPay获取api_alipay_open_auth_token_app一直提示验签错误 #115

Closed LiamChan closed 4 years ago

LiamChan commented 4 years ago

ISVAliPay获取api_alipay_open_auth_token_app一直提示验签错误,'sub_code':` 'isv.invalid-signature', 'sub_msg': '验签出错,建议检查签名字符串或签名私钥与应用公钥是否匹配,

我把私钥和公钥证书全部都重新申请了一次还是不行。也不知道是哪里出了什么问题。

# app_private_key_string = open("/Users/ys/Documents/支付宝签名Csr/jetpet_public.txt").read()
alipay_public_key_string = open("/Users/ys/Downloads/alipayCertPublicKey.crt").read()
# app_public_key_string = open("/Users/ys/Downloads/appCertPublicKey.crt").read()

app_private_key_string = """-----BEGIN RSA PRIVATE KEY-----
MIIEpAIBAAKCAQEArHDRTDNvRigrffKT2SqmQD0xLpbD9N13HHxpcuJALem5wEMomSHxNmnG+fcqNuQ9dlf6edHsadixKyyFJX4NUHUIkC2cSU7JH+uiftR4f5i7l29cA37Gx1wwZj5CfRoXRE18cQteV728lfMKrjK1L6n8vH6lic4LkbeWIUKEDzzpl1GoCIxUCFSG2U2QtjZpCge6DtjGYw9RoWsZOvFLdBBXOOalffSdNGo+30vy4DOOydGDAysOyHRleAGdhWuDd08CjUakAd9TJv7IW1gTYwZJtiTd5uWEbxiFCKs042feMvKNGYq7hK3RoyfjcTFThTlOBEmWfbZKUKUA/Kmo9QIDAQABAoIBAQCRxKyopbIitEJPrYedxJfVJeU1ZO0xSSUvR1w8aVWnejQzepIPIMovHSu+kll1HWt3Ztetsu8OascZoADNuMi4grZCasv6jEcoXKB4ZlOWSR4K3XAC5GBUlnGzfF3oMu4IJxhS1zFhKlsIxvL4XpUbPa8QCF8XJr5Q58hZRUktNwQeiteC2oJ4NleJT4COFd7IqPBP0JIbxlh1Cl13vAR5VOximDB/6OUYb3qvjpl+lZTsvj6sDyLTzBx95JiXLBzasthHioDm/d7nzEIzxdQl99StnKLu9wymPBxOauMrU2dz+OVXrRlgzqiri97M/Gu8MZLxaQ+n0QgII0sW63cBAoGBAOS7cMzcfcsvdA9V6hphV2BTVCkNfoNOaox+WL3BFD1C4PKUptJUNUcgyFHoB4eheN1d/tDJMPI16plf0lAk1dnBpKcURwE0w7E9q6iXJwR3eIdMKBWCpJLhKh2aClgF0zVh5WrrUl+nOqVQJrLpffxwqcCw4QcV2Z3u3dIDG78NAoGBAMD/ciW/B76yPfV2kLOr+VUYaGgwjlciSFTsylVBey7YwJyvwxUJ6XTA6kGzKsurWBWhHZObUpR7QO7VrNnWHpvjp4i1uSx/jkdHIjTy2DBSOylq1ORu7ZojiQcCWrDrYEz0idsW4eMQnN2cN1+DBxrubDcRG3DnSSx4Ddm3r1eJAoGBAJCV19j3q+DLb07eXSwtygDOy81jhUxVPk7u8wOH0FZlNzLAOq+LoV4D3/y+cjwPGwq7hv7Nns4MvO4UvyoADtomaQFgdP3TL7yTIjZgzb4qExtbTZ9DROq/wFKT8+PuUrNU3aN3e0uRtyWWg8/mfsgbI4L1jFlhO3105+0AIALlAoGAB8WBhDGYDz7ALfUzSHjhVmLgOWnTIYEY5oqObzZuzmkeAHE3as52XyQxiqm7TG4+4sgVF8+GWMJJ0BQ8nJz5qI9XPLMMnNUaFpavsvAgcnmtNjK+FeC9oppRtz2KB1vDCfZC4taeC/WsS2Qx+p+dFqGJLAJ9V2OI18iRzjWOUvkCgYBUVDBlo2ykFn7o4FWKcHKe4YlDFgrTXFVakrm6W7yNoAlOhBV160uISL/oKvXXbf20lW5IegABA2RNIGw5nCyu7nCK0wMQGfVYz4jQel8/OygY0UDNSUgaUFUTJF/cyVL3EgZXtvDNPbNq148gQ5qAkbsGVvYt4x7ZrT4kB+Q==
-----END RSA PRIVATE KEY-----"""

print(app_private_key_string)
print(alipay_public_key_string)
isv_alipay = ISVAliPay(
    appid="2021001197680777",
    app_notify_url=None,  # 默认回调url
    app_private_key_string=app_private_key_string,
    # 支付宝的公钥,验证支付宝回传消息使用,不是你自己的公钥,
    alipay_public_key_string=alipay_public_key_string,
    sign_type="RSA2",
    debug=False,
    app_auth_code='3f2c0bf703654839bfd937d34078eX34aaa',
    app_auth_token=None
)
print(isv_alipay)
response = isv_alipay.api_alipay_open_auth_token_app()
print('^^^response^^^\n', response)
fzlee commented 4 years ago

抱歉, 最近比较忙没有时间回复。

在支付宝开放平台的沙箱环境申请应用。 在各项信息配置好之后, 访问如下链接

https://openauth.alipaydev.com/oauth2/appToAppAuth.htm?app_id={你的APPID}&redirect_uri=http%3A%2F%2F127.0.0.1

然后使用买家信息登录并授权。可以获取app_auth_code

然后使用如下示例的代码, 可以获取到app_auth_token

from datetime import datetime

from alipay import AliPay, ISVAliPay

private_key_string = open("certs/app_private_key.pem").read()
public_key_string = open("certs/ali_public_key.pem").read()
isv = ISVAliPay(
    appid="你的appid",
    app_notify_url="http://114.229.67.15:8088/billalipay",
    app_private_key_string=private_key_string,
    alipay_public_key_string=public_key_string,
    sign_type="RSA2",  # RSA or  RSA2
    debug=True,
    app_auth_code="你的code"
)

response = isv.api_alipay_open_auth_token_app()
print(response)

可以获取如下响应:

{
  "code": "10000",
  "msg": "Success",
  "app_auth_token": "202010BB4436ccd26f6345d0b66260f3adc6eX69",
  "app_refresh_token": "202010BBfd8ee2e7d1d0434194c8ad8cbc634X69",
  "auth_app_id": "2021000120624007",
  "expires_in": 31536000,
  "re_expires_in": 32140800,
  "tokens": [
    {
      "app_auth_token": "202010BB4436ccd26f6345d0b66260f3adc6eX69",
      "app_refresh_token": "202010BBfd8ee2e7d1d0434194c8ad8cbc634X69",
      "auth_app_id": "2021000120624007",
      "expires_in": 31536000,
      "re_expires_in": 32140800,
      "user_id": "2088102181399695"
    }
  ],
  "user_id": "2088102181399695"
}
fzlee commented 4 years ago

你可以先测试一下

LiamChan commented 4 years ago

你可以先测试一下

麻烦你了,十分感谢。但是我通过支付宝证书方式还是有问题,我换成了密钥的方式就可以了。