How do I access the linkedin API? - python

I am trying to access linkedin API using python code. Here's the code that I am running on my windows machine:
from linkedin import server
import webbrowser
API_KEY = "<API_KEY>"
API_SECRET = "<API_SECRET>"
application = server.quick_api(API_KEY, API_SECRET)
I am executing these statements one-by-one in the console, but when I execute server.quick_api(API_KEY, API_SECRET), I receive the following error:
>>> application = server.quick_api(API_KEY, API_SECRET)
https://www.linkedin.com/uas/oauth2/authorization?scope=r_basicprofile%20rw_nus%20r_network%20r_contactinfo%20w_messages%20rw_groups%20r_emailaddress%20r_fullprofile&state=0b0290ff6e51e14c5409434a6e4bf52f&redirect_uri=http%3A//localhost%3A8000/&response_type=code&client_id=<API_KEY>
Traceback (most recent call last):
File "C:\Python27\lib\site-packages\IPython\core\interactiveshell.py", line 2731, in run_code
exec code_obj in self.user_global_ns, self.user_ns
File "<ipython-input-9-eb867f0fb231>", line 1, in <module>
application = server.quick_api(API_KEY, API_SECRET)
File "C:\Users\sony\AppData\Roaming\Python\Python27\site-packages\linkedin\server.py", line 24, in quick_api
_wait_for_user_to_enter_browser(app)
File "C:\Users\sony\AppData\Roaming\Python\Python27\site-packages\linkedin\server.py", line 38, in _wait_for_user_to_enter_browser
httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
File "C:\Python27\lib\SocketServer.py", line 419, in __init__
self.server_bind()
File "C:\Python27\lib\BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python27\lib\SocketServer.py", line 430, in server_bind
self.socket.bind(self.server_address)
File "C:\Python27\lib\socket.py", line 224, in meth
return getattr(self._sock,name)(*args)
error: [Errno 10013] An attempt was made to access a socket in a way forbidden by its access permissions
I get a url which I understand I am supposed to get, but what's the cause of this error?
Also, when I paste the url in the browser, it takes me to linkedin page and asks for my credentials which it never accepts (I am sure I am entering the correct credentials). It always shows:
"HTTP Error 404. The requested resource is not found."
I am otherwise able to login to linkedin using the same credentials.

Related

IIS flask is not able to run admin privilege commands

I am stuck in this setup for almost a week now. Hope that someone can guide me through it.
Setup
I have setup an IIS Server running Flask python code. (Using wfastcgi.py )
I have configured the Application Pool Identity to my own account. (Admin Permission)
I have changed all the files permission that are needed for this web deployment to "Everyone" - Full Control(Read,Write,Execute). (I understand the security risks, this is my staging environment.)
Web server is running fine and i have checked using the bottom code to know my python permission is administrator.
def am_i_admin():
try:
is_admin = os.getuid() == 0
except AttributeError:
is_admin = ctypes.windll.shell32.IsUserAnAdmin() != 0
if is_admin == True:
return "ADMIN"
else:
return "USER"
Problem Statement
I am trying to run administrator priv code on my flask IIS server which allow user within the same network to execute; such as
subprocess.run(['ipconfig'], stdout=subprocess.PIPE)
pyautogui.screenshot() #which take a screenshot of the web server and send over to the client.
I ran on my local jupyter notebook, and the above functions worked perfectly well.
But it failed to run on the IIS flask server.
I have also tried to setup pyautogui on flask server (stand alone without IIS), it worked.
What is the issue with the IIS server ?? Or are there more things that i need to configure. Are there security features that I can disable ?
Subprocess error message:
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 600, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\my_app.py", line 58, in <module>
out = os.popen("ipconfig").read()
File "c:\users\aspnet\anaconda3\lib\os.py", line 990, in popen
bufsize=buffering)
File "c:\users\aspnet\anaconda3\lib\subprocess.py", line 753, in __init__
errread, errwrite) = self._get_handles(stdin, stdout, stderr)
File "c:\users\aspnet\anaconda3\lib\subprocess.py", line 1090, in _get_handles
errwrite = _winapi.GetStdHandle(_winapi.STD_ERROR_HANDLE)
OSError: [WinError 6] The handle is invalid
StdOut:
StdErr:
pyautogui error:
Error occurred while reading WSGI handler:
Traceback (most recent call last):
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 791, in main
env, handler = read_wsgi_handler(response.physical_path)
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 633, in read_wsgi_handler
handler = get_wsgi_handler(os.getenv("WSGI_HANDLER"))
File "c:\users\aspnet\anaconda3\lib\site-packages\wfastcgi.py", line 600, in get_wsgi_handler
handler = __import__(module_name, fromlist=[name_list[0][0]])
File ".\my_app.py", line 45, in <module>
pyautogui.screenshot()
File "c:\users\aspnet\anaconda3\lib\site-packages\pyscreeze\__init__.py", line 135, in wrapper
return wrappedFunction(*args, **kwargs)
File "c:\users\aspnet\anaconda3\lib\site-packages\pyscreeze\__init__.py", line 427, in _screenshot_win32
im = ImageGrab.grab()
File "c:\users\aspnet\anaconda3\lib\site-packages\PIL\ImageGrab.py", line 44, in grab
include_layered_windows, all_screens
OSError: screen grab failed
StdOut:
StdErr:
File "c:\users\aspnet\anaconda3\lib\site-packages\PIL\ImageGrab.py", line 44, in grab
include_layered_windows, all_screens
OSError: screen grab failed
To resolve the issue set stderr and stdin to subprocess.PIPE:
['where', 'wkhtmltopdf'], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE).communicate()[0].strip()
Reference: https://github.com/foliojs/pdfkit/issues/714
File "c:\users\aspnet\anaconda3\lib\site-packages\PIL\ImageGrab.py", line 44, in grab
include_layered_windows, all_screens
OSError: screen grab failed
Use below code:
from PIL import ImageGrab
OR
from PIL import Image

"Address already in use" Error while using LinkedIn API

I was trying to use Linkedin API to scrape information about companies but i had a socket error:
Traceback (most recent call last):
File "/Users/anhvangiang/Desktop/PY/test.py", line 10, in <module>
application = server.quick_api(key, secret)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/linkedin/server.py", line 25, in quick_api
_wait_for_user_to_enter_browser(app)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/linkedin/server.py", line 39, in _wait_for_user_to_enter_browser
httpd = BaseHTTPServer.HTTPServer(server_address, MyHandler)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 417, in __init__
self.server_bind()
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/SocketServer.py", line 431, in server_bind
self.socket.bind(self.server_address)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 48] Address already in use
My code is:
from linkedin import server
from socket import *
sock = socket()
sock.setsockopt(SOL_SOCKET, SO_REUSEADDR, 1)
key = "key"
secret = "secret"
application = server.quick_api(key, secret)
Any suggestions how can i fix the problem ?
You could find and kill the process using the:
ps aux | grep python
Get the process id and kill using:
kill -9 PID
It will free the address.
OR you can try :
application = server.quick_api(key, secret, 5557)

GAE/Flask [Errno 13] Permission denied

As the title suggests, I am seeing this error when my flask app tries to run.
I am hosting the application locally using dev_appserver.
The error occurs when I visit the site and it tries to run the app. It appears that GAE is trying and failing to bind a socket for some reason.
I suspect that this may have something to do with OAuth2. Maybe it requires an SSL connection?
I don't even know where to begin solving this as none of the other posts about this are experiencing the same variation of the issue.
Edit: Here's a screenshot of the console confirming that the GAE server launches successfully on a different port; still doesn't resolve it
Traceback (most recent call last):
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 240, in Handle
handler = _config_handle.add_wsgi_middleware(self._LoadHandler())
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 299, in _LoadHandler
handler, path, err = LoadObject(self._handler)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\runtime\wsgi.py", line 85, in LoadObject
obj = __import__(path[0])
File "C:\Users\XXX\PycharmProjects\ad-assignment\main.py", line 51, in <module>
app.run()
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\flask\app.py", line 843, in run
run_simple(host, port, self, **options)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 694, in run_simple
inner()
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 656, in inner
fd=fd)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 550, in make_server
passthrough_errors, ssl_context, fd=fd)
File "C:\Users\XXX\PycharmProjects\ad-assignment\lib\werkzeug\serving.py", line 464, in __init__
HTTPServer.__init__(self, (host, int(port)), handler)
File "C:\Python27\Lib\SocketServer.py", line 417, in __init__
self.server_bind()
File "C:\Python27\Lib\BaseHTTPServer.py", line 108, in server_bind
SocketServer.TCPServer.server_bind(self)
File "C:\Python27\Lib\SocketServer.py", line 431, in server_bind
self.socket.bind(self.server_address)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\dist27\socket.py", line 222, in meth
return getattr(self._sock,name)(*args)
File "C:\Users\XXX\AppData\Local\Google\Cloud SDK\google-cloud-sdk\platform\google_appengine\google\appengine\api\remote_socket\_remote_socket.py", line 676, in bind
raise _SystemExceptionFromAppError(e)
error: [Errno 13] Permission denied
INFO 2016-12-16 21:41:51,631 module.py:788] default: "GET /oauth2callback?code=x/xxxxxxxxxxxxxxxxx HTTP/1.1" 500 -
Code (as seen in Google's OAuth2 usage guide):
import flask
app = flask.Flask(__name__)
#app.route('/')
def index():
...
#app.route('/oauth2callback')
def oauth2callback():
...
if __name__ == 'main':
import uuid
app.secret_key = str(uuid.uuid4())
app.debug = False
app.run()
We have a tutorial that walks you through adding Firebase Authentication to your Python app running with Flask. Firebase Authentication is the preferred identity toolkit now. You can of course still use a pure OAuth2 flow, but Firebase Auth also provides multi-provider authentication if that's something you were considering adding to your app anyways. If you just want to dive into the sample's code its here on GitHub.
If you just want to stick with straight OAuth, you might want to look at your Flask code itself. Getting flask to run is pretty easy on App Engine. My guess is that you're calling some code that you don't need to (flask.run()) or you aren't importing your library properly (see appengine_config.py).

py2neo (Neo4j) : py2neo.packages.httpstream.http.SocketError: Operation not permitted

I am running Neo4j 2.2.1 in ubuntu Amazon EC2 instance. When I am trying to connect through python using py2neo-2.0.7, I am getting following error :
py2neo.packages.httpstream.http.SocketError: Operation not permitted
I am able to access the web-interface through http://52.10.**.***:7474/browser/
CODE :-
from py2neo import Graph, watch, Node, Relationship
url_graph_conn = "https://neo4j:password#52.10.**.***:7474/db/data/"
print url_graph_conn
my_conn = Graph(url_graph_conn)
babynames = my_conn.find("BabyName")
for babyname in babynames:
print 2
Error message :-
https://neo4j:password#52.10.**.***:7474/db/data/
Traceback (most recent call last):
File "C:\Users\rharoon002\eclipse_workspace\peace\peace\core\graphconnection.py", line 39, in <module>
for babyname in babynames:
File "C:\Python27\lib\site-packages\py2neo\core.py", line 770, in find
response = self.cypher.post(statement, parameters)
File "C:\Python27\lib\site-packages\py2neo\core.py", line 667, in cypher
metadata = self.resource.metadata
File "C:\Python27\lib\site-packages\py2neo\core.py", line 213, in metadata
self.get()
File "C:\Python27\lib\site-packages\py2neo\core.py", line 258, in get
response = self.__base.get(headers=headers, redirect_limit=redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 966, in get
return self.__get_or_head("GET", if_modified_since, headers, redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 943, in __get_or_head
return rq.submit(redirect_limit=redirect_limit, **kwargs)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 433, in submit
http, rs = submit(self.method, uri, self.body, self.headers)
File "C:\Python27\lib\site-packages\py2neo\packages\httpstream\http.py", line 362, in submit
raise SocketError(code, description, host_port=uri.host_port)
py2neo.packages.httpstream.http.SocketError: Operation not permitted
You are trying to access neo4j via https on the standard port for http (7474):
url_graph_conn = "https://neo4j:password#52.10.**.***:7474/db/data/"
The standard port for a https connection is 7473. Try:
url_graph_conn = "https://neo4j:password#52.10.**.***:7473/db/data/"
And make sure you can access the web interface via https:
https://52.10.**.***:7473/browser/
You can change/see the port settings in your neo4j-server.properties file.

Python Dropbox Core API: TypeError when completing authorization

I'm using manual on https://www.dropbox.com/developers/core/start/python .
Have made everything equal to manual, including creation of my app in account, copy-past of app keys, allowing to use the app using the key (in fact I open the link in my browser, click allow and copy confirmation code).
After this, I want to finish authorization, but I get such error text:
>>> access_token, user_id = flow.finish(code)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1233, in finish
return self._finish(code, None)
File "/usr/local/lib/python2.7/dist-packages/dropbox/client.py", line 1101, in _finish
response = self.rest_client.POST(url, params=params)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 316, in POST
return cls.IMPL.POST(*n, **kw)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 254, in POST
post_params=params, headers=headers, raw_response=raw_response)
File "/usr/local/lib/python2.7/dist-packages/dropbox/rest.py", line 218, in request
preload_content=False
File "/usr/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 112, in urlopen
conn = self.connection_from_host(u.host, port=u.port, scheme=u.scheme)
File "/usr/lib/python2.7/dist-packages/urllib3/poolmanager.py", line 84, in connection_from_host
pool = pool_cls(host, port, **self.connection_pool_kw)
TypeError: __init__() got an unexpected keyword argument 'ssl_version'
P.S. Flow object is alive => http://screencloud.net/v/nDi0
Is seems you use urllib3 version 1.5 or older. Upgrade it to 1.6 or 1.7.

Categories

Resources