How to draw geometry in 3d View window with bgl
Using a modal operator is the simplest and especially the correct way to draw something with bgl.

import bpy
import bgl
import blf
import gpu
from gpu_extras.batch import batch_for_shader


def draw_line_3d(color, start, end):
    shader = gpu.shader.from_builtin('3D_UNIFORM_COLOR')
    batch = batch_for_shader(shader, 'LINES', {&quot;pos&quot;: [start,end]})
    shader.bind()
    shader.uniform_float(&quot;color&quot;, color)
    batch.draw(shader)

def draw_typo_2d(color, text):
    font_id = 0
    blf.position(font_id, 20, 70, 0)
    blf.color(font_id, color[0], color[1], color[2], color[3])
    blf.size(font_id, 20, 72)
    blf.draw(font_id, text)


def draw_callback_3d(operator, context):
    
    # object locations
    cube_loc = context.scene.objects['Cube'].location
    lamp_loc = context.scene.objects['Light'].location
    camera_loc = context.scene.objects['Camera'].location
    
    # 80% alpha, 2 pixel width line
    bgl.glEnable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_LINE_SMOOTH)
    bgl.glEnable(bgl.GL_DEPTH_TEST)
    
    # green line   
    draw_line_3d((0.0, 1.0, 0.0, 0.7), camera_loc, cube_loc)

    # red line
    draw_line_3d((1.0, 0.0, 0.0, 0.7), cube_loc, lamp_loc)

    # blue line
    draw_line_3d((0.0, 0.0, 1.0, 0.7), lamp_loc, camera_loc)
    
    bgl.glEnd()
    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glDisable(bgl.GL_LINE_SMOOTH)
    bgl.glEnable(bgl.GL_DEPTH_TEST)


def draw_callback_2d(self, context):

    bgl.glEnable(bgl.GL_BLEND)
    draw_typo_2d((1.0, 0.0, 0.0, 1.0), &quot;Hello Word&quot;)
    bgl.glEnd()
    
    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_DEPTH_TEST)
    

class ModalDrawOperator(bpy.types.Operator):
    bl_idname = &quot;view3d.modal_operator&quot;
    bl_label = &quot;Simple Modal View3D Operator&quot;

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_3d, 'WINDOW')
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_2d, 'WINDOW')
            return {'CANCELLED'}

        return {'PASS_THROUGH'}
        #return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle_3d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_3d, args, 'WINDOW', 'POST_VIEW')
            self._handle_2d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_2d, args, 'WINDOW', 'POST_PIXEL')

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, &quot;View3D not found, cannot run operator&quot;)
            return {'CANCELLED'}


def register():
    bpy.utils.register_class(ModalDrawOperator)


def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)

if __name__ == &quot;__main__&quot;:
    register()

Documentation: https://www.blender.org/api/current/bgl.html#module-bgl

Blender 2.7x

Click to enlarge
import bpy
import bgl
import blf

def draw_line_3d(color, start, end, width=1):
    bgl.glLineWidth(width)
    bgl.glColor4f(*color)
    bgl.glBegin(bgl.GL_LINES)
    bgl.glVertex3f(*start)
    bgl.glVertex3f(*end)

def draw_typo_2d(color, text):
    font_id = 0  # XXX, need to find out how best to get this.
    # draw some text
    bgl.glColor4f(*color)
    blf.position(font_id, 20, 70, 0)
    blf.size(font_id, 20, 72)
    blf.draw(font_id, text)


def draw_callback_3d(self, context):
    bgl.glEnable(bgl.GL_BLEND)
      
    # object locations
    cube_loc = context.scene.objects['Cube'].location
    lamp_loc = context.scene.objects['Light'].location
    camera_loc = context.scene.objects['Camera'].location

    # green line   
    draw_line_3d((0.0, 1.0, 0.0, 0.7), camera_loc, cube_loc)
    
    # red line
    draw_line_3d((1.0, 0.0, 0.0, 0.7), cube_loc, lamp_loc)
    
    # blue line
    draw_line_3d((0.0, 0.0, 1.0, 0.7), lamp_loc, camera_loc, 2)
    
    bgl.glEnd()
    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)

def draw_callback_2d(self, context):
    
    bgl.glEnable(bgl.GL_BLEND)

    # draw text
    draw_typo_2d((1.0, 1.0, 1.0, 1), &quot;Hello Word &quot;)

    bgl.glEnd()
    # restore opengl defaults
    bgl.glLineWidth(1)
    bgl.glDisable(bgl.GL_BLEND)
    bgl.glEnable(bgl.GL_DEPTH_TEST)
    bgl.glColor4f(0.0, 0.0, 0.0, 1.0)


class ModalDrawOperator(bpy.types.Operator):
    bl_idname = &quot;view3d.modal_operator&quot;
    bl_label = &quot;Simple Modal View3D Operator&quot;

    def modal(self, context, event):
        context.area.tag_redraw()

        if event.type in {'RIGHTMOUSE', 'ESC'}:
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_3d, 'WINDOW')
            bpy.types.SpaceView3D.draw_handler_remove(self._handle_2d, 'WINDOW')
            return {'CANCELLED'}

        return {'PASS_THROUGH'}
        #return {'RUNNING_MODAL'}

    def invoke(self, context, event):
        if context.area.type == 'VIEW_3D':
            # the arguments we pass the the callback
            args = (self, context)
            # Add the region OpenGL drawing callback
            # draw in view space with 'POST_VIEW' and 'PRE_VIEW'
            self._handle_3d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_3d, args, 'WINDOW', 'POST_VIEW')
            self._handle_2d = bpy.types.SpaceView3D.draw_handler_add(draw_callback_2d, args, 'WINDOW', 'POST_PIXEL')

            context.window_manager.modal_handler_add(self)
            return {'RUNNING_MODAL'}
        else:
            self.report({'WARNING'}, &quot;View3D not found, cannot run operator&quot;)
            return {'CANCELLED'}


def register():
    bpy.utils.register_class(ModalDrawOperator)


def unregister():
    bpy.utils.unregister_class(ModalDrawOperator)

if __name__ == &quot;__main__&quot;:
    register()