xdevplatform-samples

repository·main·Indexed 25 days ago

https://github.com/xdevplatform/samples

Working code samples for the X API v2 across multiple programming languages, including Python, JavaScript, Ruby, Java, and R. The repository provides examples for various API functionalities such as Posts, Users, Timelines, Streams, Spaces, Direct Messages, Media, and Compliance, with detailed setup instructions for authentication using Bearer Tokens, OAuth 2.0, and OAuth 1.0a.

Tokens
5.6K
Snippets
13
Records
19
Agent score
84%

What's inside xdevplatform-samples

  1. Configure environment variables for X API v2 authentication

    main

    Set the required environment variables based on the type of API operation you are performing:

    For read-only operations (e.g., search, lookup): Requires BEARER_TOKEN.

    For user actions (e.g., post, like, repost, bookmark): Requires OAuth 2.0 credentials: CLIENT_ID and CLIENT_SECRET.

    # For read-only operations
    export BEARER_TOKEN='your_bearer_token'
    
    # For user actions
    export CLIENT_ID='your_client_id'
    export CLIENT_SECRET='your_client_secret'
  2. Configure environment variables for X API v2

    main

    The Python examples require environment variables for authentication. The required variables depend on the type of operation you are performing:

    Read-only operations

    For operations like search or lookup, set the BEARER_TOKEN:

    export BEARER_TOKEN='your_bearer_token'

    User actions

    For operations involving user actions (e.g., post, like, repost, bookmark, mute, etc.), you must use OAuth 2.0 authentication by setting CLIENT_ID and CLIENT_SECRET:

    export CLIENT_ID='your_client_id'
    export CLIENT_SECRET='your_client_secret'
    # For read-only
    export BEARER_TOKEN='your_bearer_token'
    
    # For user actions
    export CLIENT_ID='your_client_id'
    export CLIENT_SECRET='your_client_secret'
  3. Add Maven dependencies for X API v2 Java samples

    main

    Add the following dependencies to your pom.xml to support HTTP requests and JSON parsing used in the samples.

    <dependencies>
        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
            <version>2.9.1</version>
        </dependency>
    </dependencies>
  4. Run X API v2 Examples

    main

    Navigate to the language-specific directory, install dependencies, and run the desired script. Below are the commands for each supported language.

    # Python
    cd python && pip install -r requirements.txt
    python posts/search_recent.py
    
    # JavaScript
    cd javascript
    node posts/search_recent.js
    
    # Ruby
    cd ruby && bundle install
    ruby posts/search_recent.rb
    
    # Java
    cd java
    javac -cp ".:lib/*" posts/RecentSearchDemo.java
    java -cp ".:lib/*" RecentSearchDemo
  5. Configure environment variables for X API v2 Ruby examples

    main

    The examples require different environment variables depending on the type of operation being performed. Set these in your shell before running the scripts.

    Read-only operations

    Use these for search and lookup tasks:

    • BEARER_TOKEN

    User actions

    Most user actions (posting, liking, reposting, bookmarking) require OAuth 2.0 authentication:

    • CLIENT_ID
    • CLIENT_SECRET

    OAuth 1.0a (Legacy endpoints)

    Use these for examples targeting legacy endpoints:

    • CONSUMER_KEY
    • CONSUMER_SECRET
    # For read-only (search, lookup)
    export BEARER_TOKEN='your_bearer_token'
    
    # For user actions (post, like, etc.)
    export CLIENT_ID='your_client_id'
    export CLIENT_SECRET='your_client_secret'
    
    # For OAuth 1.0a
    export CONSUMER_KEY='your_consumer_key'
    export CONSUMER_SECRET='your_consumer_secret'
  6. Run X API v2 Ruby examples

    main

    Once dependencies are installed and environment variables are exported, you can run any example script using the ruby command. For example, to run a recent search script:

    # Make sure environment variables are set
    ruby posts/search_recent.rb