How can I bake f-curve using python for the active object?

running bpy.ops.graph.bake() in the console rises a context error so i changed the context using one of CoDEmanX seggestions as follows :
import bpy
area = bpy.context.area
old_type = area.type
obj = bpy.context.active_object
curves = obj.animation_data.action.fcurves
for curve in curves:
curve.select = True
area.type = 'GRAPH_EDITOR'
bpy.ops.graph.bake()
area.type = old_type
this will bake all the F-curve of the active object