Shale oil resources have enormous potential and broad development prospects in China. Especially with the rapid development of horizontal well volume fracturing technology, the production of shale oil has been greatly increased, and it has become an important position to increase oil and gas storage and production. However, due to the existence of artificial and natural fractures produced by fracturing, gas channeling will occur in the process of huff-n-puff gas injection, which will affect the production and lead to poor development effect. In this paper, foam is used as an anti-channeling agent in the process of huff-n-puff gas injection. The results of microfluidic experiments show that the production mode of foam in porous media is liquid membrane hysteresis and liquid membrane separation. In porous media, foam first blocks large channels such as fractures, and then enters the matrix. The oil recovery mechanism of foam assisted gas injection in porous media is to improve the sweep efficiency and oil displacement efficiency of gas. Compared with N2 huff-n-puff, the oil recovery of the first cycle of foam assisted N2 huff-n-puff increased by 4.50%, and the third cycle increased by 9.58%. It is proved that foam has good anti channeling effect in gas huff-n-puff injection process. The research results provide an effective method for efficient gas injection development of shale oil.