77from .save import SaveSearchResults
88from .markdown import MarkdownRenderer
99
10+ # Required for OAuth
11+ import json
12+ import webbrowser
13+ from oauthlib .oauth2 import MobileApplicationClient
14+ from requests_oauthlib import OAuth2Session
15+
1016console = Console ()
1117
1218
@@ -85,4 +91,38 @@ def get_ans(self, questions_list):
8591
8692 console .print (output_text )
8793 ans .append (json_ans_data ["items" ])
88- return ans
94+ return ans
95+
96+ # Get an access token and extract to a JSON file "access_token.json"
97+ def setCustomKey (self ):
98+ client_id = 20013
99+
100+ # scopes possible values:
101+ # read_inbox - access a user's global inbox
102+ # no_expiry - access_token's with this scope do not expire
103+ # write_access - perform write operations as a user
104+ # private_info - access full history of a user's private actions on the site
105+ scopes = 'read_inbox'
106+
107+ authorization_url = 'https://stackoverflow.com/oauth/dialog'
108+ redirect_uri = 'https://stackexchange.com/oauth/login_success'
109+
110+ # Create an OAuth session and open the auth_url in a browser for the user to authenticate
111+ stackApps = OAuth2Session (client = MobileApplicationClient (client_id = client_id ), scope = scopes , redirect_uri = redirect_uri )
112+ auth_url , state = stackApps .authorization_url (authorization_url )
113+ print (f"Visit this page in your browser: { auth_url } " )
114+ webbrowser .open (auth_url )
115+
116+ # Extract the access token data and save to a variable
117+ callback_url = input ("Paste URL after authenticating here: " )
118+ accessTokenData = stackApps .token_from_fragment (callback_url )
119+
120+ # Store the access token data in a JSON file
121+ jsonDict = {
122+ "access_token" : accessTokenData ['access_token' ],
123+ "expires" : accessTokenData ['expires' ],
124+ "state" : accessTokenData ['state' ]
125+ }
126+
127+ with open ('access_token.json' , 'w' ) as jsonFile :
128+ json .dump (jsonDict , jsonFile )
0 commit comments