对接qq互联平台的网站应用,在pyramid中采用重定向到qq connect OAuth2的authorize:
#导向qq connect authority页面
@view_config(route_name='qq.auth', renderer='json')
def qqauth_view(request):
url = OauthClient.get_authorize_url()
print("qq.auth.url:",url)
return HTTPFound(location=url )
在独立部署下没有问题,可以让浏览器302重定向到:
https://graph.qq.com/oauth2.0/authorize?response_type=code&client_id=xxxx&redirect_uri=http://www.momoji.com.cn/qq/callback&scope=get_user_info,&state=
但采用iis的url rewrite之后,浏览器收到的response header 中Location为http://www.momoji.com.cn/,重定向的url为:http://www.momoji.com.cn/oauth2.0/authorize?response_type=code&client_id=xxxx&redirect_uri=http://www.momoji.com.cn/qq/callback&scope=get_user_info,&state=
应该是iis url rewrite的问题,尝试从pyramid.httpexceptions.HTTPFound类入手看有没有固定response header location为 https://graph.qq.com的方法,没有找到;
思考,若是换一种部署比如nginx做反向代理,难道也要改代码么?不至于啊,还是回到iis url rewrite设置上;
尝试如下规则,放置于第一行,经测试有效:
<rule name="https://graph.qq.com/oauth2.0/authorize" stopProcessing="true">
<match url="oauth2.0/authorize?(.*)" />
<conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
<action type="Redirect" url="https://graph.qq.com/oauth2.0/authorize?{R:1}" redirectType="Found" />
</rule>
即:若匹配到url域名/之后部分为oauth2.0/authorize?(.*)则重定向到https://graph.qq.com/oauth2.0/authorize?{R:1}这里;
这里踩了一个坑,明确了,在匹配正则表达式时,不是对整个url写,而是对域名/之后的部分进行匹配;另,包括.?这些字符不需要转义(转义也可).