Teach you to use proxy IP to play OAuth2.0 authentication
Brothers engaged in network development should have encountered this situation: local debugging interface is always limited by the server access. Especially when doing OAuth2.0 authentication.Frequent token requests from the same IPIf you have a proxy IP, you will be blacklisted in a minute. If you know how to use a proxy IP, things will be much simpler.
Why do I need a proxy IP for authentication?
For example, your company has 10 programmers are debugging the same platform API, the platform found that a large number of requests from the same public IP, direct blocking 24 hours. At this time with ipipgo's dynamic proxy pool, each person is assigned a different exit IP, equivalent to each developer set a layer of "invisibility cloak".
Typical scenario for requesting a token using a proxy IP
import requests
proxies = {
'http': 'http://user:pass@proxy.ipipgo.cn:9011',
'https': 'http://user:pass@proxy.ipipgo.cn:9011'
}
response = requests.post(
'https://api.service.com/oauth/token',
proxies=proxies,
data={'grant_type': 'client_credentials'}
)
Four Steps to Real-World Configuration
Step 1: Choose the right type of agent
Getting OAuth 2.0 suggests using ipipgo'sLong-lasting static proxiesThe authentication process needs to maintain the session state, dynamic IP may drop out in the middle of the process. Here is a small pit to note: some platforms will check the IP geographic location, remember to choose the target server location node.
Step 2: Authentication Parameter Setting
from requests_oauthlib import OAuth2Session
oauth = OAuth2Session(
redirect_uri='Callback address',
proxies={
'http': 'http://ipipgo_user:密码@gateway.ipipgo.cn:9020',
'https': 'http://ipipgo_user:密码@gateway.ipipgo.cn:9020'
}
)
Step 3: Exception retry mechanism
Add a retry policy to the requests to automatically change the proxy IP when it encounters the 429 status code. it is recommended to dynamically switch nodes with the ipipgo API, which can be written in the code:
from urllib3.util.retry import Retry
from requests.adapters import HTTPAdapter
retries = Retry(
retries = Retry(
total=3, backoff_factor=0.5, status_forcelist=[429, 502, 503
status_forcelist=[429, 502, 503]
)
session = requests.Session()
session.proxies.update(proxies)
session.mount('https://', HTTPAdapter(max_retries=retries))
A guide to stepping through the pits (QA session)
Q:After the proxy is set up, it still prompts connection timeout?
A: First check that the proxy address is not written incorrectly, especially the password with special symbols remember the URL code. Use the test interface provided by ipipgo console to verify the connectivity.
Q: Getting token prompts invalid_grant?
A: This situation may be caused by the time zones of different proxy nodes are not synchronized, add X-Timestamp field in the request header, use UTC timestamp alignment
Q: How to handle high concurrency scenarios?
A: on ipipgo's enterprise version of the package, support for simultaneous binding of 50 export IP, in the code to do an IP polling pool, automatic switching to avoid triggering frequency limitations
Let's get real.
When I first started to use proxy IP for authentication, I always found all kinds of configuration troublesome. Later found that with the ready-made SDK provided by ipipgo, in fact, three lines of code can be done:
from ipipgo.oauth_proxy import OAuthProxy
proxy = OAuthProxy(api_key="your key")
token = proxy.get_token() Automatically handle proxy rotation and retries
Now their new users send 1G flow, enough to test. The key is the technical support response is fast, last time I encountered a cold platform certificate problem, the engineer directly remote help me debug through.