I am assuming you are doing this outside of Pro, Desktop or a Web Application Builder type app and directly inserting via API.
Python example:
def get_token():
#-------------------------------------------------------------------------------
# Name: Function -
# Purpose:
#-------------------------------------------------------------------------------
data = checklist.b64decode(AGOL_Pass)
pw = data.decode("ascii")
url = 'https://www.arcgis.com/sharing/rest/generateToken'
values = {'f': 'json',
'username': AGOL_User,
'password': pw,
'referer' : 'https://www.arcgis.com',
'expiration' : '10'}
data = urllib.parse.urlencode(values).encode("utf-8")
req = urllib.request.Request(url)
response = None
while response is None:
try:
response = urllib.request.urlopen(req,data=data)
except:
pass
the_page = response.read()
#Garbage Collection with some house building
payload_json = the_page.decode('utf8')
payload_json = json.loads(payload_json)
edit_token = payload_json['token']
return (edit_token)
If you are trying to do this through a web app as an example though, you can take the route of getting a long term token. In this case though, there are some perils involved depending on how you configure things. You would tack on ?token={}, {} being the token you would insert for the service. This would allow you to use a Portal account as authentication for that feature service.