Exemple #1
0
def upload_to_google(email,fileid,title="New Document from DriveStack",mimetype="text/plain",description="this has been uploaded by Drivestack"):
    
    all_data = GoogleData.objects.filter(email=email).values()[0]
    #return HttpResponse(str(all_data))
    credentials = OAuth2Credentials(str(all_data["access_token"]),str(all_data["client_id"]),str(all_data["client_secret"]),str(all_data["refresh_token"]),str(all_data["token_expiry"]),str(all_data["token_uri"]),str(all_data["user_agent"]))
    try:
        service = builds_service(credentials)
    except:
        access_token = refresh_google_token(email)
        credentials = OAuth2Credentials(access_token,str(all_data["client_id"]),str(all_data["client_secret"]),str(all_data["refresh_token"]),str(all_data["token_expiry"]),str(all_data["token_uri"]),str(all_data["user_agent"]))
        service = builds_service(credentials)
        service_handler = ServiceHandler()
    f = open(os.path.join(upload_path,file_id),'r')
    content = f.read()
    f.close()
    try:
        file_id = service_handler.post(service,mime_type,content,title,description)
    except:
        
        access_token = refresh_google_token(email)
        credentials = OAuth2Credentials(access_token,str(all_data["client_id"]),str(all_data["client_secret"]),str(all_data["refresh_token"]),str(all_data["token_expiry"]),str(all_data["token_uri"]),str(all_data["user_agent"]))
        service = builds_service(credentials)
        service_handler = ServiceHandler()
        file_id = service_handler.post(service,mime_type,content,title,description)
    #file_content  =service_handler.service,file_id)
    return file_id
Exemple #2
0
def download_from_google(email,file_id):
    all_data = GoogleData.objects.filter(email=email).values()[0]
    #return HttpResponse(str(all_data))
    credentials = OAuth2Credentials(str(all_data["access_token"]),str(all_data["client_id"]),str(all_data["client_secret"]),str(all_data["refresh_token"]),str(all_data["token_expiry"]),str(all_data["token_uri"]),str(all_data["user_agent"]))
    try:
        service = builds_service(credentials)
        service_handler = ServiceHandler()
        response = service_handler.get(service,file_id)
    except:
        access_token = refresh_google_token(email)
        credentials = OAuth2Credentials(access_token,str(all_data["client_id"]),str(all_data["client_secret"]),str(all_data["refresh_token"]),str(all_data["token_expiry"]),str(all_data["token_uri"]),str(all_data["user_agent"]))
        service = builds_service(credentials)
        service_handler = ServiceHandler()
        response = service_handler.get(service,file_id)
    content = response['content']
    title = response['title']
    f=open(os.path.join(upload_path,file_id),'w')
    f.write(content)
    f.close()
    return title