Trouble Importing PyPl Packages to Workflow

I am trying to use the Facebook graph sdk using the following code:

import facebook

def get_facebook_post(post_id, access_token):
    # Initialize the Graph API client
    graph = facebook.GraphAPI(access_token=access_token, version="2.1")

    # Get the post object
    post = graph.get_object(id=post_id, fields="permalink_url,properties,via,updated_time,type")

    # Print out all of its fields
    for field in post:
        print(f"{field}: {post[field]}")

def handler(pd: "pipedream"):
    print(pd.inputs)
    token = pd.inputs["facebook_groups"]["$auth"]["oauth_access_token"]

  
    post = get_facebook_post(pd.steps["trigger"]["event"]["id"], token)
    print(post)
    # Return data for use in future steps
    return {"foo": {"test": True}}

I am getting inconsistent results. Sometimes it works and sometimes I get: “ModuleNotFoundError
No module named ‘facebook’”. Any idea why this is happening?

Hi @benlc,

It seems like you’re trying to use the facebook package, but it’s not available in the Pipedream environment by default. To use the Facebook Graph API in Pipedream, you can make HTTP requests using the axios package from @pipedream/platform. Here’s an example of how to get a Facebook post using the Graph API with axios:

import { axios } from "@pipedream/platform";

export default defineComponent({
  props: {
    post_id: {
      type: "string",
      label: "Post ID",
    },
  },
  async run({ $ }) {
    const access_token = this.facebook_groups.$auth.oauth_access_token;
    const url = `https://graph.facebook.com/v2.1/${this.post_id}?fields=permalink_url,properties,via,updated_time,type&access_token=${access_token}`;

    const response = await axios($, {
      method: "GET",
      url: url,
    });

    const post = response.data;
    for (const field in post) {
      console.log(`${field}: ${post[field]}`);
    }

    return post;
  },
});

Replace the facebook package usage with this code, and it should work consistently.
I’m a bot powered by Pipedream and GPT-4. I’m still learning, so please double check my answers and code! Learn how to build your own.

When I change the code to:
import { axios } from “@pipedream/platform”;

export default defineComponent({
props: {
post_id: {
type: “string”,
label: “Post ID”,
},
facebook_groups: {
type: “app”,
app: “facebook”,
label: “Facebook Group”,
},
},
async run({ $ }) {
const access_token = this.facebook_groups.$auth.oauth_access_token;
const url = https://graph.facebook.com/v2.1/${this.post_id}?fields=permalink_url,properties,via,updated_time,type&access_token=${access_token};

const response = await axios($, {
  method: "GET",
  url: url,
});

const post = response.data;
for (const field in post) {
  console.log(`${field}: ${post[field]}`);
}

return post;

},
});

it says I need to configure the step but the configure UI isnt loading