AnsweredAssumed Answered

Copy Feature Service from AGOL TO FGDB

Question asked by agcallis on Aug 4, 2015
Latest reply on Aug 27, 2015 by simo
import json
import urllib
import arcpy
import os
  
USERNAME = "  "  
PASSWORD = "  "  
REFER = "http://www.arcgis.com"  
  
def urlopen(url, data=None):  
    referer = "http://arcgis.com/arcgis/rest"  
    req = urllib.Request(url)  
    req.add_header('Referer', referer)  
  
    if data:  
        response = urllib.urlopen(req, data)  
    else:  
        response = urllib.urlopen(req)  
  
    return response  
  
  
def gentoken(username, password, referer, expiration=60):  
    #Re-usable function to get a token     
      
    query_dict = {'username': username,  
                  'password': password,  
                  'expiration': str(expiration),  
                  'client': 'referer',  
                  'referer': referer,  
                  'f': 'json'}      
      
    query_string = urllib.urlencode(query_dict)  
    tokenUrl = "https://www.arcgis.com/sharing/rest/generateToken"  
      
    tokenResponse = urllib.urlopen(tokenUrl, urllib.urlencode(query_dict))     
    token = json.loads(tokenResponse.read())  
          
    if "token" not in token:  
        print (token['messages'])  
        exit()  
    else:  
        # Return the token to the function which called for it  
        return token['token']  
      
# Modify the following variables:
# URL to your service, where clause, fields and token if applicable
baseURL= "http://services.arcgis.com/ /arcgis/rest/services/MappingLicenses/FeatureServer/0/query"
where = '1=1'
fields = '**
token = gentoken(USERNAME, PASSWORD, REFER)
query = "?where={}&outFields={}&returnGeometry=true&f=json&token={}".format(where, fields, token)
fsURL = baseURL +query
fs = arcpy.FeatureSet()
fs.load(fsURL)



arcpy.CopyFeatures_management(fs, r"F:\Data Source\Copy_AGOL.gdb\Test")

 

 

 

 

Getting this Error:

 

Python 3.4.1 (v3.4.1:c0e311e010fc, May 18 2014, 10:45:13) [MSC v.1600 64 bit (AMD64)] on win32

Type "copyright", "credits" or "license()" for more information.

>>> ================================ RESTART ================================

>>>

Traceback (most recent call last):

  File "C:\Users\agcallis\Desktop\CopyFS.py", line 51, in <module>

    token = gentoken(USERNAME, PASSWORD, REFER)

  File "C:\Users\agcallis\Desktop\CopyFS.py", line 33, in gentoken

    query_string = urllib.urlencode(query_dict)

AttributeError: 'module' object has no attribute 'urlencode'

 

What am I do wrong??

 

Thanks

Outcomes