Saturday, September 17, 2022

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 :