gpt4 book ai didi

Twitter API Redirects After Authorization But Won't Give Me a PIN(Twitter API在授权后重定向,但不给我PIN)

转载 作者:bug小助手 更新时间:2023-10-25 10:37:57 26 4
gpt4 key购买 nike



I am trying to block a user account with the Twitter API through Python. Here's what my method looks like:

我正在尝试通过Python阻止具有Twitter API的用户帐户。下面是我的方法:


def block_user():

payload = {"target_user_id": "amazon"}

# Get request token
request_token_url = "https://api.twitter.com/oauth/request_token"
oauth = OAuth1Session(consumer_key, client_secret=consumer_secret,)

try:
fetch_response = oauth.fetch_request_token(request_token_url)
except ValueError:
print(
"There may have been an issue with the consumer_key or consumer_secret you entered."
)

resource_owner_key = fetch_response.get("oauth_token")
resource_owner_secret = fetch_response.get("oauth_token_secret")
print("Got OAuth token: %s" % resource_owner_key)

# Get authorization
base_authorization_url = "https://api.twitter.com/oauth/authorize"
authorization_url = oauth.authorization_url(base_authorization_url)
print("Please go here and authorize: %s" % authorization_url)
verifier = input("Paste the PIN here: ")

# Get the access token
access_token_url = "https://api.twitter.com/oauth/access_token"
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=resource_owner_key,
resource_owner_secret=resource_owner_secret,
verifier=verifier,
# I tried this, it didn't work
#oauth_callback='oob',
)
oauth_tokens = oauth.fetch_access_token(access_token_url)

access_token = oauth_tokens["oauth_token"]
access_token_secret = oauth_tokens["oauth_token_secret"]

# Make the request
oauth = OAuth1Session(
consumer_key,
client_secret=consumer_secret,
resource_owner_key=access_token,
resource_owner_secret=access_token_secret,
)

# Making the request
response = oauth.post(
"https://api.twitter.com/2/users/{}/blocking".format(id), json=payload
)

if response.status_code != 200:
raise Exception(
"Request returned an error: {} {}".format(response.status_code, response.text)
)

print("Response code: {}".format(response.status_code))

# Saving the response as JSON
json_response = response.json()
print(json.dumps(json_response, indent=4, sort_keys=True))

In the terminal when I run the script, I get this:

在终端中,当我运行脚本时,我得到这样的结果:


Got OAuth token: 9SL...BGo
Please go here and authorize: https://api.twitter.com/oauth/authorize?oauth_token=9SL...BGo
Paste the PIN here:

So I go to the URL and it asks me to authorize the app. I authorize it, and then get redirected immediately to the redirect page of the Twitter API app settings (default.com in this case). I don't get shown a PIN to put into the prompt, I don't see any success message or anything, it just redirects immediately. Can't find anyone else with that issue using traditional search terms.

所以我转到URL,它要求我授权该应用程序。我授权它,然后立即被重定向到Twitter API应用程序设置的重定向页面(在本例中为default.com)。我没有看到要在提示符中输入PIN,我没有看到任何成功消息或任何东西,它只是立即重定向。使用传统搜索词找不到其他有这个问题的人。


Any ideas?

有什么主意吗?


更多回答
优秀答案推荐

You need to add the following URL parameters to your auth/request_token URL. For me, that meant changing this line (the third line in my code block):

您需要将以下URL参数添加到您的auth/请求_令牌URL中。对我来说,这意味着更改此行(我的代码块中的第三行):


request_token_url = "https://api.twitter.com/oauth/request_token"

to this:

对此:


request_token_url = "https://api.twitter.com/oauth/request_token?oauth_callback=oob&x_auth_access_type=write"

I had tried using oauth_callback=oob in a few other places including my OAuth1Session object and on the authentication URL. Needs to be on the request_token URL.

我曾尝试在其他一些地方使用OAUTH_CALLBACK=OOB,包括我的OAuth1Session对象和身份验证URL。需要位于REQUEST_TOKEN URL上。


更多回答

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com