Saturday, September 17, 2022

Google API Generate Access Token and Refresh Token

Steps :

a) Generate client id and secret key.

b) Add Google logged in user as a Test user in consent screen.

c) Generate Authorization Code (One Time)

https://accounts.google.com/o/oauth2/auth?client_id=xxxxxxxx&redirect_uri=http://localhost&response_type=code&scope=https://www.googleapis.com/auth/drive&access_type=offline
d) Generate Refresh Token (One Time)
curl --request POST --data "code=xxxxxxxx&client_id=xxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&redirect_uri=http://localhost&grant_type=authorization_code" https://oauth2.googleapis.com/token
e) Generate Access Token from Refresh token (Always)
curl --request POST --data "client_id=xxxxxxxxxxx&client_secret=xxxxxxxxxxxxxxx&refresh_token=xxxxxxxxxxxxx&grant_type=refresh_token" https://oauth2.googleapis.com/token

Note : Refresh Tokens expire in 1 week if your app is not set as production. Change publishing status of your app from testing to production to use your refresh token always.

The Publishing Status option can be found in 'Oauth Consent Screen' which is under API & Services.


 

 


Microsoft API - Get Access Token and Refresh Token

1. Log into Azure Portal Active Directory.

2. Register your application.

3. Create secret for your application.

4. Find your Client ID and Tenant ID.

5. Generate Authorization  Code. (One Time)

https://login.microsoftonline.com/

{Tenant ID}/oauth2/v2.0/authorize?client_id={AppReg ID} &response_type=code &redirect_uri=http%3a%2f%2flocalhost%3a8080 &response_mode=query &scope={AppReg ID}%2f.default&state=12345&sso_reload=true 

6. Save redirect url. It is Authorization Response.  

7. Generate Refresh Token. (One Time)

curl -X POST -H 

"Content-Type: application/x-www-form-urlencoded" 

-d 'client_id={AppReg ID}
  &scope={AppReg ID}%2f.default openid profile offline_access
  &code={authorization code}
  &redirect_uri=http%3A%2F%2Flocalhost%3a8080
  &grant_type=authorization_code
  &client_secret={AppReg Secret}' 

'https://login.microsoftonline.com/{Tenant ID}/oauth2/v2.0/token'

8. Generate Access Token from Refresh Token (Every Time)

curl --location --request

POST 'https://login.microsoftonline.com/

{Tenant ID}/oauth2/v2.0/token' \ --header 'Content-Type: application/x-www-form-urlencoded' \ --data-urlencode 'client_id={AppReg ID}' \ --data-urlencode 'scope={scope returned in previous request}' \ --data-urlencode 'refresh_token={Refresh Token}' \ --data-urlencode 'grant_type=refresh_token' \ --data-urlencode 'client_secret={AppReg Secret}'
 




Friday, September 9, 2022

Error Telethon - TypeError: 'ChannelParticipants' object is not subscriptable

When you are trying to export members of a Telegram public group using python script and you get following error

Traceback (most recent call last):
  File "t1_exportgroup.py", line 49, in <module>
    all_participants = client.get_participants(target_group, aggressive=False)
  File "/home/user/virtualenvs/telegram/lib/python3.8/site-packages/telethon/requestiter.py", line 74, in __anext__
    if await self._load_next_chunk():
  File "/home/user/virtualenvs/telegram/lib/python3.8/site-packages/telethon/client/chats.py", line 223, in _load_next_chunk
    participants = results[i]
TypeError: 'ChannelParticipants' object is not subscriptable

Solution :

You are getting this error because group has more than 5k or 6K members, you need to provide limit in your python script.

all_participants = []
all_participants = client.get_participants(target_group, aggressive=False, limit=5000)

Now try again, you should be able to export members less than 5000.

 Watch the video to see the demo :