Example #1
0
def get_access_token():
	token_str = keychain.get_password('dropbox', app_key)
	if token_str:
		key, secret = pickle.loads(token_str)
		return session.OAuthToken(key, secret)
	request_token = get_request_token()
	sess = session.DropboxSession(app_key, app_secret, access_type)
	access_token = sess.obtain_access_token(request_token)
	token_str = pickle.dumps((access_token.key, access_token.secret))
	keychain.set_password('dropbox', app_key, token_str)
	return access_token
Example #2
0
def dropbox_authrized(request):
    back_url = reverse(index)

    uid = request.GET.get('uid', '')
    oauth_token = request.GET.get('oauth_token', '')
    if not uid and not oauth_token:
        info = u'Dropbox 认证出问题了 :( 麻烦再一遍。'
        return render_to_response('info.html', {
            'info': info,
            'interval': 3,
            'back_url': back_url
        })

    request_key = request.session.get('request_key')
    request_secret = request.session.get('request_secret')
    if not request_key and not request_secret:
        info = u'请打开浏览器cookie支持。'
        return render_to_response('info.html', {
            'info': info,
            'interval': 3,
            'back_url': back_url
        })

    request_token = session.OAuthToken(request_key, request_secret)
    sess = session.DropboxSession(settings.DROPBOX_APP_KEY,
                                  settings.DROPBOX_APP_SECRET,
                                  settings.DROPBOX_ACCESS_TYPE)
    access_token = sess.obtain_access_token(request_token)

    user, is_created = DropboxUser.objects.get_or_create(uid=uid)
    user.access_key = access_token.key
    user.access_secret = access_token.secret
    #client = client = client.DropboxClient(sess)
    #info = client.account_info()
    user.save()

    request.session['dropbox_uid'] = uid

    info = 'Ok, Dropbox 认证成功 :)'
    return render_to_response('info.html', {
        'info': info,
        'interval': 3,
        'back_url': back_url
    })
def get_access_token():
    if not os.path.exists('dbturret_token'):
        sess = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
        request_token = sess.obtain_request_token()
     
        url = sess.build_authorize_url(request_token)
        print "url:", url
        print "Please visit this website and press the 'Allow' button, then hit 'Enter' here."
        raw_input()
     
        access_token = sess.obtain_access_token(request_token)
     
        with open("dbturret_token", 'w') as f:
            f.write(access_token.key+chr(0)+access_token.secret)
     
    with open("dbturret_token") as f:
        key, secret = f.read().strip().split(chr(0))
        access_token = session.OAuthToken(key, secret)
        return access_token
Example #4
0
def main():
    error = False
    if len(argv) < 4:
        print "Missing arguments. Usage:", argv[0], "<app secret>", \
        "<access token> <command> [<args>]"
        exit(0)

    global APP_SECRET, ACCESS_TOKEN
    APP_SECRET = argv[1]
    access_token_string = argv[2]
    command = argv[3]

    dsession = session.DropboxSession(APP_KEY, APP_SECRET, ACCESS_TYPE)
    # if access token has ben specified, set it
    if access_token_string != "none":
        l = access_token_string.split('_')
        if len(l) < 2:
            print "Error:Invalid access token"
            exit(0)
        key = l[0]
        secret = l[1]
        dsession.set_token(key, secret)

    dclient = client.DropboxClient(dsession)

    try:
        if command == "authorize_url":
            request_token = get_request_token(dsession)
            print ":Request token:" + request_token.key + "_" + request_token.secret + ":"
            print ":URL:" + get_authorize_url(dsession, request_token) + ":"
        elif command == "create_access_token":
            if len(argv) >= 5:
                token_string = argv[4]
            else:
                token_string = "invalid_invalid"
            l = token_string.split('_')
            key = l[0]
            secret = l[1]
            request_token = session.OAuthToken(key, secret)
            access_token = get_access_token(dsession, request_token)
            print ":Access token:" + access_token.key + "_" + access_token.secret + ":"
        elif command == "user_name":
            print ":User:"******":"
        elif command == "upload_file":
            if len(argv) < 6:
                print "Error:Arguments missing in upload_file()"
                exit(0)
            src = argv[4]
            dest = argv[5]
            response = upload_file(dclient, src, dest)
            print response
        elif command == "upload_file_chunked":
            if len(argv) < 6:
                print "Error:Arguments missing in upload_file_chunked()"
                exit(0)
            src = argv[4]
            dest = argv[5]
            upload_file_chunked(dclient, src, dest)
        elif command == "download_file":
            if len(argv) < 6:
                print "Error:Arguments missing in download_file()"
                exit(0)
            src = argv[4]
            dest = argv[5]
            response = download_file(dclient, src, dest)
            print response
        elif command == "delete_file":
            if len(argv) < 5:
                print "Error:Arguments missing in delete_file()"
                exit(0)
            delete_file(dclient, argv[4])
            print "Delete:OK"
        else:
            print "Error:Invalid command"

    except Exception, e:
        print "Error:" + str(e)
        exit(0)
Example #5
0
 def link2(self):
     with open('entry.pickle', 'rb') as f:
         (akey,asecret) = pickle.load(f)
         request_token = session.OAuthToken(akey,asecret)
     self.obtain_access_token(request_token)
     self.write_creds(self.token)