# 创建Actor和后端交互 **Published by:** [Kjsghuh](https://paragraph.com/@kjsghuh/) **Published on:** 2022-06-13 **URL:** https://paragraph.com/@kjsghuh/actor ## Content 首先:图 一如图一,创建一个Actor需要调用 dfinity/agent里面的Actor类中的createActor方法,传入对应后端的did.js文件中的idl Factory,agent和canisterId;其中agent如下图生成:图 二Http Agent也是dfinity/agent里面的类,只需传入一个identity(用authClient里面的getIdentity方法),authClient如下图生成(AuthClient为官方接口)图 三但是,如果你不登陆的话,get Identity将是匿名的用户,所以在create之后还得调用login方法:图 四login之后getIdentity就是认证过后的Identity,生成的agent也是认证后的;用idl Factory、agent和canister ID就可以生成Actor调用后端函数、最后:调用后端,创建actor,login这些东西可以分别用类来封装,这样的话,不同的api可以调同一个类里面的createActor方法。 ## Publication Information - [Kjsghuh](https://paragraph.com/@kjsghuh/): Publication homepage - [All Posts](https://paragraph.com/@kjsghuh/): More posts from this publication - [RSS Feed](https://api.paragraph.com/blogs/rss/@kjsghuh): Subscribe to updates