Mailchimp Oauth2返回400 invalid_client

问题描述

代码基本上是从mailchimp oauth docs复制粘贴的,tokenResponse返回400个无效的客户端。客户端ID和密码已被三重检查。

from urllib.parse import urlencode
from operator import itemgetter

import requests
from flask import Flask,redirect,request
import mailchimp_marketing as MailchimpMarketing

app = Flask(__name__)

@app.route('/')
def index():
    return "<p>Welcome to the sample Mailchimp OAuth app! Click <a href='/auth/mailchimp'>here</a> to log in</p>"


@app.route('/auth/mailchimp',methods=['GET'])
def auth():
    # You should always store your client id and secret in environment variables for security — the exception: sample code.
    MAILCHIMP_CLIENT_ID = "ID"
    oauth_base = "https://login.mailchimp.com/oauth2/authorize?"
    return redirect("{}{}".format(oauth_base,urlencode({
            "response_type": "code","client_id": MAILCHIMP_CLIENT_ID,"redirect_uri": "http://127.0.0.1:5000/auth/mailchimp/callback"
        })))


@app.route('/auth/mailchimp/callback',methods=['GET'])
def authCallback():
    MAILCHIMP_CLIENT_ID = "ID"
    MAILCHIMP_CLIENT_SECRET = "SECRET"
    BASE_URL = "http://127.0.0.1:5000"
    OAUTH_CALLBACK = "{}/oauth/mailchimp/callback".format(BASE_URL)

    code = request.args['code']

    # Here we're exchanging the temporary code for the user's access token.
    tokenResponse = requests.post("https://login.mailchimp.com/oauth2/token",data={
                 'body': {
                     'grant_type': 'authorization_code','client_id': MAILCHIMP_CLIENT_ID,'client_secret': MAILCHIMP_CLIENT_SECRET,'redirect_uri': OAUTH_CALLBACK,'code': code
                  }})
    access_token = itemgetter('access_token')(tokenResponse.json())
    print(access_token)

    MetadataResponse = requests.get(
        'https://login.mailchimp.com/oauth2/Metadata',headers={'Authorization': "OAuth {}".format(access_token)})

    dc = itemgetter('dc')(MetadataResponse.json())

    mailchimp = MailchimpMarketing.Client()
    mailchimp.set_config({
      "access_token": "YOUR_ACCESS_TOKEN","server": "YOUR_SERVER_PREFIX"
    })

    response = mailchimp.ping.get()

    return """<p>This user's access token is {} and their server prefix is {}.</p>
          <p>When pinging the Mailchimp Marketing API's ping endpoint,the server responded:<p>
           <code>{}</code>
          """.format(access_token,dc,code)
if __name__ == '__main__':
    app.run(debug=True,host='0.0.0.0')

Mailchimp支持人员说,此问题可能是由于请求为http而引起的。有没有人遇到。帮助将不胜感激.............................................. ...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)