• 0 Posts
  • 5 Comments
Joined 1 year ago
cake
Cake day: June 12th, 2023

help-circle



  • Okay, before I head off to bed, I think this works for the login and authentication token:

    import requests
    import json
    
    def login(username_or_email, password):
        # Define the URL for the login endpoint
        url = "https://lemmy.ml/api/v1/user/login"
    
        # Define the headers for the request
        headers = {'Content-Type': 'application/json'}
    
        # Define the data for the login
        data = {
         "username_or_email": username_or_email,
         "password": password
        }
    
        # Send the POST request
        response = requests.post(url, headers=headers, data=json.dumps(data))
    
        # Extract the JWT from the response
        jwt = response.json().get('jwt')
    
        return jwt
    
    # Use the login function
    jwt = login("your_username_or_email", "your_password")
    print(jwt)
    

    The JSON Web Token (jwt) should contain the authentication token. At least I think that’s the case. I picked this out by reading through the Go code at the following URL: https://github.com/Elara6331/go-lemmy/blob/master/lemmy.go

    I’ll play around with the code later.


  • Here’s another example, this time for creating a comment:

    import requests
    import json
    
    # Define the URL for the API endpoint
    url = "https://lemmy.ml/api/v1/comment"
    
    # Define the headers for the request
    headers = {'Content-Type': 'application/json'}
    
    # Define the data for the new comment
    data = {
     "content": "Your comment content",
     "post_id": 123,  # Replace with the ID of the post you're commenting on
     "form_id": "your_form_id",  # Replace with your form ID
     "auth": "your_auth_token_here"
    }
    
    # Send the POST request
    response = requests.post(url, headers=headers, data=json.dumps(data))
    
    # Print the response
    print(response.json())
    

    Does anyone know how to do the login process in Lemmy, and retrieve an auth token?