twitter frontend gql requests

mail@pastecode.io avatar
unknown
golang
a month ago
3.3 kB
4
Indexable
Never
func (c *TwitterClient) GetGuestToken() (string, error) {
	client := &http.Client{}
	// activate.json endpoint only works for POST, and user agent must be legit
	req, err := http.NewRequest("POST", "https://api.twitter.com/1.1/guest/activate.json", nil)
	if err != nil {
		return "", err
	}
	// authorization token is static value used for logged out users
	req.Header.Set("authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA")
	req.Header.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36")
	res, err := client.Do(req)
	if err != nil {
		return "", err
	}
	var resp GetGuestTokenResponse
	resBody, err := io.ReadAll(res.Body)
	if err != nil {
		return "", err
	}
	if err = json.Unmarshal(resBody, &resp); err != nil {
		return "", err
	}
	return resp.GuestToken, nil
}

func (c *TwitterClient) GetTweet(tweetUrl *TweetUrl, guestToken string) (*Tweet, error) {
	// the url's get parameter is the gql query (gql does not accept POST requests)
	url := "https://api.twitter.com/graphql/OGDXNj5PSaBRwg2MlpX0JQ/TweetResultByRestId?variables=%7B%22tweetId%22%3A%22" + tweetUrl.Id + "%22%2C%22withCommunity%22%3Afalse%2C%22includePromotedContent%22%3Afalse%2C%22withVoice%22%3Afalse%7D&features=%7B%22creator_subscriptions_tweet_preview_api_enabled%22%3Atrue%2C%22c9s_tweet_anatomy_moderator_badge_enabled%22%3Atrue%2C%22tweetypie_unmention_optimization_enabled%22%3Atrue%2C%22responsive_web_edit_tweet_api_enabled%22%3Atrue%2C%22graphql_is_translatable_rweb_tweet_is_translatable_enabled%22%3Atrue%2C%22view_counts_everywhere_api_enabled%22%3Atrue%2C%22longform_notetweets_consumption_enabled%22%3Atrue%2C%22responsive_web_twitter_article_tweet_consumption_enabled%22%3Afalse%2C%22tweet_awards_web_tipping_enabled%22%3Afalse%2C%22freedom_of_speech_not_reach_fetch_enabled%22%3Atrue%2C%22standardized_nudges_misinfo%22%3Atrue%2C%22tweet_with_visibility_results_prefer_gql_limited_actions_policy_enabled%22%3Atrue%2C%22rweb_video_timestamps_enabled%22%3Atrue%2C%22longform_notetweets_rich_text_read_enabled%22%3Atrue%2C%22longform_notetweets_inline_media_enabled%22%3Atrue%2C%22responsive_web_graphql_exclude_directive_enabled%22%3Atrue%2C%22verified_phone_label_enabled%22%3Afalse%2C%22responsive_web_media_download_video_enabled%22%3Afalse%2C%22responsive_web_graphql_skip_user_profile_image_extensions_enabled%22%3Afalse%2C%22responsive_web_graphql_timeline_navigation_enabled%22%3Atrue%2C%22responsive_web_enhance_cards_enabled%22%3Afalse%7D"
	client := &http.Client{}
	req, err := http.NewRequest("GET", url, nil)
	if err != nil {
		return nil, err
	}
	// authorization token is static frontend value used for logged out users
	req.Header.Set("authorization", "Bearer AAAAAAAAAAAAAAAAAAAAANRILgAAAAAAnNwIzUejRCOuH5E6I8xnZz4puTs%3D1Zv7ttfk8LF81IUq16cHjhLTvJu4FA33AGWWjCpTnA")
	// REQUIRED, IF YOU DO NOT PROVIDE GUEST TOKEN, YOULL GET RATE LIMITED
	req.Header.Set("x-guest-token", guestToken)
	res, err := client.Do(req)
	if err != nil {
		return nil, err
	}
	var resp Tweet
	resBody, err := io.ReadAll(res.Body)
	if err != nil {
		return nil, err
	}
	if err = json.Unmarshal(resBody, &resp); err != nil {
		return nil, err
	}
	return &resp, nil
}
Leave a Comment