Hướng dẫn python requests post bearer token - python yêu cầu đăng mã thông báo mang

Tôi đang cố gắng tạo một tập lệnh đăng dữ liệu trên dịch vụ REST cùng với mã thông báo Bearer.

Đây là ví dụ PHP hoạt động:

$postData = array( 'app' => 'aaaa' );

$ch = curl_init($apiUrl);
curl_setopt_array($ch, array(
    CURLOPT_HTTPHEADER, ['Authorization: Bearer '.$accessToken],
    CURLOPT_POST => TRUE,
    CURLOPT_RETURNTRANSFER => TRUE,
    CURLOPT_POSTFIELDS => json_encode($postData)
));

$response = curl_exec($ch);

Tôi đang cố gắng làm điều tương tự với Python. Đây là những gì tôi có được cho đến nay ...

import urllib2, urllib
import json

auth_token='kbkcmbkcmbkcbc9ic9vixc9vixc9v'
hed = {'Authorization': 'Bearer ' + auth_token}
data = urllib.urlencode({'app' : 'aaaaa'})

url = 'https://api.xy.com'
req = urllib2.Request(url=url, headers=hed, data=data)
content = urllib2.urlopen(req).read()
print content

Tôi nhận được lỗi HTTP 400: Yêu cầu xấu. Bất kỳ sự giúp đỡ nào đã có ...

Các bước để lấy dữ liệu từ API bằng Python.

  • Có được mã thông báo truy cập
  • Tạo một chỉnh sửa mới
  • Nhận chỉnh sửa mở
  • Thay thế một APK hiện có
  • Thêm một apk mới
  • Cập nhật danh sách

Có được mã thông báo truy cập

Sử dụng ID máy khách và Bí mật máy khách của bạn để có được mã thông báo Auth. Bạn sẽ thêm mã thông báo auth vào tiêu đề của mỗi yêu cầu API. Ví dụ Python sau đây cho thấy cách lấy mã thông báo Auth và tạo tiêu đề ủy quyền bằng mã thông báo.

#  Values that you need to provide
client_id = ""
client_secret = ""
app_id = ""

BASE_URL = 'https://developer.amazon.com/api/appstore'

scope = "appstore::apps:readwrite"
grant_type = "client_credentials"
data = {
    "grant_type": grant_type,
    "client_id": client_id,
    "client_secret": client_secret,
    "scope": scope
}
amazon_auth_url = "https://api.amazon.com/auth/o2/token"
auth_response = requests.post(amazon_auth_url, data=data)

# Read token from auth response
auth_response_json = auth_response.json()
auth_token = auth_response_json["access_token"]

auth_token_header_value = "Bearer %s" % auth_token

auth_token_header = {"Authorization": auth_token_header_value}

Tạo một chỉnh sửa mới

Nhận chỉnh sửa mở

create_edit_path = '/v1/applications/%s/edits' % app_id
create_edit_url = BASE_URL + create_edit_path
create_edit_response = requests.post(create_edit_url, headers=headers)
current_edit = create_edit_response.json()

edit_id = current_edit['id']

Nhận chỉnh sửa mở

Thay thế một APK hiện có

get_edits_path = '/v1/applications/%s/edits' % app_id
get_edits_url = BASE_URL + get_edits_path
get_edits_response = requests.get(get_edits_url, headers=headers)
current_edit = get_edits_response.json()

edit_id = current_edit['id']

Thay thế một APK hiện có

Thêm một apk mới

## Get the current list of APKs
get_apks_path = '/v1/applications/%s/edits/%s/apks' % (app_id, edit_id)
get_apks_url = BASE_URL + get_apks_path
apks = requests.get(get_apks_url, headers=headers)

firstAPK = apks[0]
apk_id = firstAPK['id']
replace_apk_path = '/v1/applications/%s/edits/%s/apks/%s/replace' % (app_id, edit_id, apk_id)

## Open the apk file on your local machine
local_apk = open(local_apk_path, 'rb').read()

replace_apk_url = BASE_URL + replace_apk_path
all_headers = {
    'Content-Type': 'application/vnd.android.package-archive',
    'If-Match': etag
}
all_headers.update(headers)
replace_apk_response = requests.put(replace_apk_url, headers=all_headers, data=local_apk)

Thêm một apk mới

Cập nhật danh sách

add_apk_path = '/v1/applications/%s/edits/%s/apks/upload' % (app_id, edit_id)
add_apk_url = BASE_URL + add_apk_path
local_apk = open(apk_location, 'rb').read()
all_headers = {
    'Content-Type': 'application/vnd.android.package-archive'
}
all_headers.update(headers)
add_apk_response = requests.post(add_apk_url, headers=all_headers, data=local_apk)
response = add_apk_response.json()

Cập nhật danh sách

Sử dụng ID máy khách và Bí mật máy khách của bạn để có được mã thông báo Auth. Bạn sẽ thêm mã thông báo auth vào tiêu đề của mỗi yêu cầu API. Ví dụ Python sau đây cho thấy cách lấy mã thông báo Auth và tạo tiêu đề ủy quyền bằng mã thông báo.

listing_headers = headers.copy()
listing_headers.update({
    'Content-Type': 'application/json'
})
listings_etag, current_listing_json = get_current_listing(app_id, edit_id, language, listing_headers)

edit_body = current_listing_json.copy()
edit_body.update(edit_listing_body)
edit_listing_headers = listing_headers.copy()
edit_listing_headers.update({
    'If-Match': listings_etag
})

edit_listing_response = update_listing(app_id, edit_id, language, edit_body, edit_listing_headers)

Làm thế nào để bạn gửi một yêu cầu nhận với mã thông báo mang trong Python?

Để gửi yêu cầu GET với tiêu đề ủy quyền mã thông báo của người mang bằng Python, bạn cần thực hiện yêu cầu GET HTTP và cung cấp mã thông báo cho người mang của bạn với sự ủy quyền: Bearer {token} Tiêu đề HTTP.make an HTTP GET request and provide your Bearer Token with the Authorization: Bearer {token} HTTP header.

Làm cách nào để nhận được mã thông báo xác thực trong Python?

Có được mã thông báo API để nhận mã thông báo API cho người dùng, yêu cầu POST HTTP phải được gửi đến tài nguyên mã thông báo.Trong phần thân bài đăng, tên người dùng và mật khẩu được chỉ định ở định dạng JSON và thân phản hồi chứa khóa mã thông báo có mã thông báo API thực tế là giá trị.an HTTP POST request should be sent to the Token resource. In the post body, username and password are specified in JSON format, and the response body contains a token key with an actual API Token as the value.

Làm cách nào để thêm một cơ thể vào một yêu cầu Python?

Bạn sẽ muốn điều chỉnh dữ liệu bạn gửi trong phần thân của yêu cầu của bạn với URL được chỉ định.Cú pháp: requests.post (url, data = {key: value}, json = {key: value}, tiêu đề = {key: value}, args) *(data, json, tham số tiêu đề là tùy chọn.)requests. post(url, data={key: value}, json={key: value}, headers={key:value}, args) *(data, json, headers parameters are optional.)

Làm thế nào để bạn lấy dữ liệu từ API bằng các yêu cầu Python?

Các bước để lấy dữ liệu từ API bằng Python..
Kết nối với API.Lúc đầu, chúng ta cần kết nối với API và tạo kết nối an toàn như được hiển thị bên dưới ....
Nhận dữ liệu từ API.....
Phân tích dữ liệu vào định dạng JSON.....
Trích xuất dữ liệu và in nó ..