Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Xem thảo luận
Cải thiện bài viết
Lưu bài viết
Đọc
Bàn luận
Xác thực đề cập đến việc cung cấp quyền của người dùng để truy cập vào một tài nguyên cụ thể. Vì, mọi người đều có thể được phép truy cập dữ liệu từ mỗi URL, người ta sẽ yêu cầu xác thực chủ yếu. Để đạt được xác thực này, thông thường, người ta cung cấp dữ liệu xác thực thông qua tiêu đề ủy quyền hoặc tiêu đề tùy chỉnh được xác định bởi máy chủ.
Thí dụ -
import
requests
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
0import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
1from
requests.auth
import
>>> import requests >>> from requests_oauthlib import OAuth2 >>> url = '//api.twitter.com/1.1/account/verify_credentials.json' >>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET', ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET'] >>> requests.get[url, auth=auth]1
If you an invalid username or password, it will return an error as –
>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
2>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
3>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
4 >>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
5>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
6>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
7>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
8>>> import requests
>>> from requests_oauthlib import OAuth2
>>> url = '//api.twitter.com/1.1/account/verify_credentials.json'
>>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET',
... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET']
>>> requests.get[url, auth=auth]
9
Thay thế người dùng của người dùng và người dùng và truyền qua tên người dùng và mật khẩu của bạn. Nó sẽ xác thực yêu cầu và trả về một phản hồi 200 nếu không nó sẽ trả về lỗi 403. Nếu bạn là tên người dùng hoặc mật khẩu không hợp lệ, nó sẽ trả về một lỗi là -
Another very popular form of HTTP Authentication is Digest Authentication, and Requests supports this out of the box as well:
>>> from requests.auth import HTTPDigestAuth >>> url = '//httpbin.org/digest-auth/auth/user/pass' >>> requests.get[url, auth=HTTPDigestAuth['user', 'pass']]
Các loại xác thực
A common form of authentication for several web APIs is
OAuth. The requests-oauthlib library allows Requests users to easily make OAuth 1 authenticated requests:
>>> import requests >>> from requests_oauthlib import OAuth2 >>> url = '//api.twitter.com/1.1/account/verify_credentials.json' >>> auth = OAuth2['YOUR_APP_KEY', 'YOUR_APP_SECRET', ... 'USER_OAUTH_TOKEN', 'USER_OAUTH_TOKEN_SECRET'] >>> requests.get[url, auth=auth]
DIGEST Xác thực khác nhau hình thức xác thực HTTP rất phổ biến là xác thực tiêu hóa và các yêu cầu cũng hỗ trợ điều này ra khỏi hộp:
OAuth 1 Xác thực hình thức xác thực chung cho một số API Web là OAuth. Thư viện yêu cầu-OAUthlib cho phép người dùng yêu cầu dễ dàng thực hiện các yêu cầu được xác thực OAuth 1:
The requests-oauthlib library also handles OAuth 2, the authentication mechanism underpinning OpenID Connect. See the
requests-oauthlib OAuth2 documentation for details of the various OAuth 2 credential management flows:
- Để biết thêm thông tin về cách thức hoạt động của OAuth Flow, vui lòng xem trang web OAuth chính thức. Để biết ví dụ và tài liệu về yêu cầu-OAuthlib, vui lòng xem kho lưu trữ yêu cầu_oauthlib trên GitHub
- OAuth 2 và OpenID Connect Xác thực Yêu cầu-Oauthlib cũng xử lý OAuth 2, cơ chế xác thực làm nền tảng cho OpenID Connect. Xem tài liệu Yêu cầu-OAUthlib OAuth2 để biết chi tiết về các luồng quản lý thông tin OAuth 2 khác nhau:
- Dòng ứng dụng web
- Dòng ứng dụng di động
Dòng ứng dụng kế thừa
Requests is designed to allow other forms of authentication to be easily and quickly plugged in. Members of the open-source community frequently write authentication handlers for more complicated or less commonly-used forms of authentication. Some of the best have been brought together under the Requests organization, including:
- Lưu lượng ứng dụng phụ trợ
- NTLM.
Các trình xác thực khác được thiết kế để cho phép các hình thức xác thực khác dễ dàng và nhanh chóng cắm vào. Thành viên của cộng đồng nguồn mở thường xuyên viết trình xử lý xác thực cho các hình thức xác thực phức tạp hoặc ít được sử dụng hơn. Một số điều tốt nhất đã được kết hợp với nhau theo tổ chức yêu cầu, bao gồm:
Bạn dường như đang sử dụng API giao dịch REST của IG Labs; Tài liệu nêu rõ hai chế độ xác thực, tùy thuộc vào phiên bản API bạn chỉ định. Tài liệu tiêu đề mà bạn trích dẫn là một tổng quan chung về các tiêu đề khác nhau có liên quan. Phần đó không hữu ích lắm để thực sự hiểu cách thức xác thực / ủy quyền hoạt động cho API đó.
Trên cùng một trang là một phần xác thực và ủy quyền, và đó là phần bạn cần nghiên cứu. Đây là nơi hai chế độ xác thực được mô tả. Ngoài ra còn có một phần ví dụ riêng giải thích cách hai chế độ khác nhau hoạt động với các yêu cầu và phản hồi cụ thể được hiển thị.
Để xác thực, trước tiên bạn phải gửi yêu cầu
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
2 đến tuyến import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
3, với khóa API của bạn có mặt trong tiêu đề import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
4. Đây là nơi bạn sẽ cần tên người dùng và mật khẩu. Tên người dùng / mật khẩu hợp lệ sau đó cung cấp cho bạn mã thông báo bảo mật để được sử dụng cho các yêu cầu tiếp theo. Đối với các phiên bản API 1 và 2, một trường bổ sung import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
5 có thể được đưa vào để ghi lại xem mật khẩu có phải là văn bản đơn giản hay được mã hóa hay không; Bạn chỉ cần điều này để đáp ứng các hạn chế đăng nhập IG Singapore và lĩnh vực này có thể được bỏ qua một cách an toàn.Đối với các yêu cầu V1 và V2, bạn nhận được các mã thông báo đó trong các tiêu đề phản hồi, phiên bản API V3 cung cấp cho bạn thông tin cần thiết trong cơ thể. Các mã thông báo có một thời gian hạn chế. Các mã thông báo V1/V2 có giá trị trong 6 giờ, nhưng được tự động mở rộng tới 72 giờ bằng cách đưa ra các yêu cầu với các mã thông báo đó, trước khi bạn cần đăng nhập lại. Mã thông báo V3 chỉ có giá trị trong 60 giây, nhưng
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
6 riêng cho phép bạn nhận được mã thông báo mới bằng cách gửi mã thông báo làm mới đến điểm cuối import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
7.Sau đó, các yêu cầu V1 / V2 sử dụng các mã thông báo trong cùng các tiêu đề chính xác mà phản hồi
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
3 sử dụng chúng, vì vậy hãy sao chép trên import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
9 ["Mã thông báo phiên khách"] và các tiêu đề session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
0 và bạn đang trên đường.
Yêu cầu V3 Sử dụng tiêu đề OAuth
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
1 tiêu chuẩn, với phương thức được đặt thành session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
2. Chỉ cần xây dựng tiêu đề đó với giá trị session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
3 từ cấu trúc session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
4 và lưu import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
6 cho khi session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
3 đã hết hạn. Tài liệu cũng khuyến nghị bạn đặt tiêu đề session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
7, để xác định tài khoản [mã thông báo chỉ xác định máy khách].Tôi thực sự khuyên bạn nên sử dụng một đối tượng
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
8 để làm cho việc xử lý tiêu đề đơn giản hơn.Đối với các phiên bản v1 và v2 của API, hãy sử dụng:
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
Bây giờ bạn được thiết lập để sử dụng
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
9 để tiếp tục truy cập API.Đối với V3, các mã thông báo sống trong cơ thể JSON:
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
Bây giờ bạn được thiết lập để sử dụng
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '3'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
response_data = response.json[]
oauth_tokens = response_data['oauthToken']
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
session.headers['IG-ACCOUNT-ID'] = response_data['accountId']
9 để tiếp tục truy cập API cho đến khi if response.status == 401 and response.json[]['errorCode'] == 'error.security.oauth-token-invalid':
# refresh required, old token is done.
1 đã vượt qua hoặc phản hồi 401 được đưa ra:if response.status == 401 and response.json[]['errorCode'] == 'error.security.oauth-token-invalid':
# refresh required, old token is done.
Sau đó, bạn cần sử dụng cấu trúc ____ 42oauthtoken`:
# refresh the access token
del session.headers['Authorization']
response = session.post[
url + '/session/refresh-token',
json={'refresh_token': oauth_tokens['refresh_token']},
headers={'VERSION': '1'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
oauth_tokens = response.json[]
session.headers['Authorization'] = 'Bearer ' + oauth_tokens['access_token']
Lưu ý rằng tôi tiếp tục gửi tiêu đề
if response.status == 401 and response.json[]['errorCode'] == 'error.security.oauth-token-invalid':
# refresh required, old token is done.
3 với mỗi yêu cầu riêng lẻ; API của họ sử dụng số phiên bản trên mỗi điểm kết thúc, do đó import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
3 có 3 phiên bản, nhưng import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
7 chỉ có phiên bản 1 và bạn không thể đặt if response.status == 401 and response.json[]['errorCode'] == 'error.security.oauth-token-invalid':
# refresh required, old token is done.
3 cho bất kỳ thứ gì khác hoặc nó sẽ bị hỏng.Định dạng V3
import requests
url = '//website.com'
API_KEY = '.... your API key ....'
username = 'username'
password = 'password'
session = requests.Session[]
# these are sent along for all requests
session.headers['X-IG-API-KEY'] = API_KEY
# not strictly needed, but the documentation recommends it.
session.headers['Accept'] = "application/json; charset=UTF-8"
# log in first, to get the tokens
response = session.post[
url + '/session',
json={'identifier': username, 'password': password},
headers={'VERSION': '2'},
]
response.raise_for_status[] # if not a 2xx response, raise an exception
# copy across the v1/v2 tokens
session.headers['CST'] = response.headers['CST']
session.headers['X-SECURITY-TOKEN'] = response.headers['X-SECURITY-TOKEN']
3 có vẻ cồng kềnh hơn, nhưng phiên bản đó cho phép bạn làm mới quyền truy cập của mình vô thời hạn, miễn là bạn làm như vậy trong vòng 10 phút kể từ lần sử dụng mã thông báo cuối cùng của mình; Các mã thông báo truy cập V1 / V2 hết hạn sau 72 giờ, bất cứ điều gì bạn làm.