I am new with this, I was trying to connect to the couchbase bucket using python, just tried whatever given in the documentation.
from couchbase.cluster import Cluster
from couchbase.cluster import PasswordAuthenticator
cluster = Cluster('couchbase://localhost')
authenticator = PasswordAuthenticator('username', 'password')
cluster.authenticate(authenticator)
bucket = cluster.open_bucket('bucket-name')
Replaced the IP, username, password, bucket name. But I am getting this error everytime I am trying to run this,
File "get_data_couche.py", line 6, in <module>
bucket = cluster.open_bucket('jizo')
File "/usr/lib64/python2.7/site-packages/couchbase/cluster.py", line 100, in open_bucket
rv = self.bucket_class(str(connstr), **kwargs)
File "/usr/lib64/python2.7/site-packages/couchbase/bucket.py", line 252, in __init__
self._do_ctor_connect()
File "/usr/lib64/python2.7/site-packages/couchbase/bucket.py", line 261, in _do_ctor_connect
self._connect()
couchbase.exceptions._AuthError_0x2 (generated, catch AuthError): <RC=0x2[Authentication failed. You may have provided an invalid username/password combination], There was a problem while trying to send/receive your request over the network. This may be a result of a bad network or a misconfigured client or server, C Source=(src/bucket.c,793)>
Please help me with this, not able to find so much on internet. Thanks in advance.
Related
from google.cloud import firestore
# Add a new document
db = firestore.Client() # need to put credentials
doc_ref = db.collection(u'users').document(u'alovelace')
doc_ref.set({
u'first': u'Ada',
u'last': u'Lovelace',
u'born': 1815
})
# Then query for documents
users_ref = db.collection(u'users')
for doc in users_ref.stream():
print(u'{} => {}'.format(doc.id, doc.to_dict()))
This is a example code from https://googleapis.dev/python/firestore/latest/
When I run that it shows
Traceback (most recent call last):
File "/home/kulothungan/Purp/temp/temp.py", line 4, in <module>
db = firestore.Client()
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/client.py", line 94, in __init__
client_options=client_options,
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/firestore_v1/base_client.py", line 128, in __init__
_http=None,
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 277, in __init__
_ClientProjectMixin.__init__(self, project=project, credentials=credentials)
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 225, in __init__
project = self._determine_default(project)
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/client.py", line 244, in _determine_default
return _determine_default_project(project)
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/cloud/_helpers.py", line 186, in _determine_default_project
_, project = google.auth.default()
File "/home/kulothungan/.local/lib/python3.6/site-packages/google/auth/_default.py", line 483, in default
raise exceptions.DefaultCredentialsError(_HELP_MESSAGE)
google.auth.exceptions.DefaultCredentialsError: Could not automatically determine credentials. Please set GOOGLE_APPLICATION_CREDENTIALS or explicitly create credentials and re-run the application. For more information, please see https://cloud.google.com/docs/authentication/getting-started
but I don't know how to initialize that and also I don't want to do that by using environmental variables
If anyone knows pls inform me
Thanks in advance
If you have access to this particular project, you need to go to the Google cloud console Service Accounts page here and create a service account for accessing firestore. Service account creation. Give the service account one of the datastore roles as appopriate.
Once created, you will have to generate keys for the service account. You can do this from the "Keys" tab on the service account's page.
Select the JSON format. You'll get a file called keys.json. Store that on your computer and create the environment variable GOOGLE_APPLICATION_CREDENTIALS
export GOOGLE_APPLICATION_CREDENTIALS=/path/to/keys.json
Now try running your python script.
Without using Environment Variables
You need to install the google-auth python package which provides the google.oauth2.service_account module in which there is the Credentials class. You can initialize this class with your credentials like this:
from google.oauth2 import service_account
credentials = service_account.Credentials.from_service_account_file(
'/path/to/keys.json')
Then you can pass this credentials object to the firestore Client() like this:
db = firestore.Client(credentials=credentials)
We are currently trying to access a folder of an Office 365 ProPlus tenant using the official OneDrive SDK for Python (https://github.com/OneDrive/onedrive-sdk-python). One of our clients would like to use a OneDrive folder as a way of storing and sharing programmatically generated files, therefore, we would like to provide basic file operations.
We have a working solution for a personal OneDrive account, however, when we try to apply the same approach for their OneDrive, we face an issue during the authentication process.
We asked them to register the application in the Azure AD following the steps in the official documentation. Next, they sent us the redirect URI, client ID and client secret that we included in our script. We are trying to use the following code:
redirect_uri = 'REDIRECT_URI'
client_secret = 'CLIENT_SECRET'
client_id='CLIENT_ID'
discovery_uri = 'https://api.office.com/discovery/'
auth_server_url='https://login.microsoftonline.com/common/oauth2/authorize'
auth_token_url='https://login.microsoftonline.com/common/oauth2/token'
http_provider = onedrivesdk.HttpProvider()
auth_provider = onedrivesdk.AuthProvider(http_provider,
client_id,
auth_server_url=auth_server_url,
auth_token_url=auth_token_url)
auth_url = auth_provider.get_auth_url(redirect_uri)
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
However, we get the following error message when executing the last line:
Traceback (most recent call last):
File "onedrive-test.py", line 25, in
code = GetAuthCodeServer.get_auth_code(auth_url, redirect_uri)
File "/home/username/.local/lib/python3.6/site-packages/onedrivesdk/helpers/GetAuthCodeServer.py",
line 60, in get_auth_code
s = GetAuthCodeServer((host_address, port), code_acquired, GetAuthCodeRequestHandler)
File "/home/username/.local/lib/python3.6/site-packages/onedrivesdk/helpers/GetAuthCodeServer.py",
line 76, in init
HTTPServer.init(self, server_address, RequestHandlerClass)
File "/usr/lib/python3.6/socketserver.py", line 453, in init
self.server_bind()
File "/usr/lib/python3.6/http/server.py", line 136, in server_bind
socketserver.TCPServer.server_bind(self)
File "/usr/lib/python3.6/socketserver.py", line 467, in server_bind
self.socket.bind(self.server_address)
socket.gaierror: [Errno -2] Name or service not known
We also tried opening the auth_url manually, which took us one step further, but still could not authenticate the application with the following error:
AADSTS50020: User account 'USER ACCOUNT' from identity provider
'live.com' does not exist in tenant 'TENANT NAME' and cannot access
the application 'CLIENT ID' in that tenant. The account needs to be
added as an external user in the tenant first. Sign out and sign in
again with a different Azure Active Directory user account.
We have two questions:
What might casue the first error? This is the comment (see below) that can be found in the readme of the SDK about using the GetAuthCodeServer class. It seems to us that the server cannot be run. Are there any not explicitly defined dependencies that we should be aware of before trying to run the webserver? (We are running the script on Ubuntu 18.10)
If you want to remove some of that manual work, you can
use the helper class GetAuthCodeServer. That helper class spins up a
webserver, so this method cannot be used on all environments.
With respect to the second issue, can you recommend proper material for configuring OneDrive for Business for our use-case? We went through a lot of documentation, but after long hours of research, we still could not find the correct way to fix that issue, especially since we do not have direct acces to the tenant and we cannot easily experiment with things. We would need to give a step-by-step cookbook to our client to set up everything on their side.
Any help would be much appreciated! :)
I am trying to retrieve access token using azure user id and password .
Initially I tried with the following python code block
import adal
context = adal.AuthenticationContext(AUTHORITY)
token = context.acquire_token_with_client_credentials(
"https://management.azure.com/",
CLIENT_ID,
CLIENT_SECRET)
This is returning the token without any issue .
I am following an example from https://github.com/AzureAD/azure-activedirectory-library-for-python for retrieving the token using username and password and the code block is the the following
token2 = context.acquire_token_with_username_password("https://management.azure.com/",USER_NAME,PASSWORD,CLIENT_ID)
In this case ,no token is returned , instead following response is returned .
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 145, in acquire_token_with_username_password
return self._acquire_token(token_func)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 109, in _acquire_token
return token_func(self)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\authentication_context.py", line 143, in token_func
return token_request.get_token_with_username_password(username, password)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 286, in get_token_with_username_password
token = self._get_token_username_password_federated(username, password)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 252, in _get_token_username_password_federated
username, password)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 211, in _perform_username_password_for_access_token_exchange
username, password)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\token_request.py", line 198, in _perform_wstrust_exchange
result = wstrust.acquire_token(username, password)
File "F:\All_Python\Python_Setup\Python27\lib\site-packages\adal\wstrust_request.py", line 160, in acquire_token
raise AdalError(return_error_string, error_response)
adal.adal_error.AdalError: WS-Trust RST request returned http error: 500 and server response: <s:Envelope xmlns:s="http://www.w3.org/2003/05/soap-envelope" xmlns:a="http://www.w3.org/2005/08/addressing" xmlns:u="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd"><s:Header><a:Action s:mustUnderstand="1">http://www.w3.org/2005/08/addressing/soap/fault</a:Action><o:Security s:mustUnderstand="1" xmlns:o="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd"><u:Timestamp u:Id="_0"><u:Created>2017-06-07T12:12:56.567Z</u:Created><u:Expires>2017-06-07T12:17:56.567Z</u:Expires></u:Timestamp></o:Security></s:Header><s:Body><s:Fault><s:Code><s:Value>s:Sender</s:Value><s:Subcode><s:Value xmlns:a="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-secext-1.0.xsd">a:FailedAuthentication</s:Value></s:Subcode></s:Code><s:Reason><s:Text xml:lang="en-US">ID3242: The security token could not be authenticated or authorized.</s:Text></s:Reason></s:Fault></s:Body></s:Envelope>
Process finished with exit code 1
If anybody has any idea on this, please let me know .
According to the information of error stack, per my experience, due to the error comes from the method _get_token_username_password_federated and 500 error code for WS-Trust RST request, it seems that the user/password you used for acquiring token not created in the Azure AD of your app registed, but looks like that created in a federated AD.
Please try to do the two ways below for inspection the issue.
Create a new user in the Azure AD of your app registed on Azure portal with your admin accout, then use the new user/password for retrieving the token.
Inspect your Azure AD configuration to make sure whether deployed Active Directory Federation Services and the current use/password created in a federated AD instance.
I'm using this guide:
https://learn.microsoft.com/en-us/azure/cloud-services/cloud-services-python-how-to-use-service-management.
I'm doing exact what they wrote in the guide and I keep getting error message.
>>> from azure import *
>>> from azure.servicemanagement import *
>>> subscription_id = '************************'
>>> import os
>>> os.path.isfile(r'c:\key\mycert.pem')
True
>>> certificate_path = r'c:\key\mycert.pem'
>>> sms = ServiceManagementService(subscription_id, certificate_path)
>>> result = sms.list_locations()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementservice.py", line 1131, in list_locations
Locations)
File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 365, in _perform_get
response = self.perform_get(path, x_ms_version)
File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 175, in perform_get
response = self._perform_request(request)
File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 339, in _perform_request
return _management_error_handler(ex)
File "C:\python27\lib\site-packages\azure\servicemanagement\servicemanagementclient.py", line 419, in _management_error_handler
return _general_error_handler(http_error)
File "C:\python27\lib\site-packages\azure\servicemanagement\_common_error.py", line 34, in _general_error_handler
raise AzureHttpError(message, http_error.status)
azure.common.AzureHttpError: Forbidden
<Error xmlns="http://schemas.microsoft.com/windowsazure" xmlns:i="http://www.w3.org/2001/XMLSchema-instance"><Code>ForbiddenError</Code><Message>The server failed to authenticate the request. Verify that the certificate is valid and is associated with this subscription.</Message></Error>
I've uploaded the mycert.cer to a cloud service in my azure portal.
someone have an idea why the problem is?
I'm sure that the pem and the cer files are ok.
According to your error information & the offical document of Service Management Status and Error Codes, the issue reason as the error information said as below.
Per my experience, I think there are two reasons which will cause the issue.
The certificate you current used is invalid on Azure. Besides re-generate a new certificate refered to the offical document, you also can follow the document of Azure SDK for Python about Using the Azure .PublishSettings certificate to create the client certificate.
Using the Azure .PublishSettings certificate
You can download your Azure publish settings file and use the certificate that is embedded in that file to create the client certificate. The server certificate already exists, so you won’t need to upload one.
The server certificate file did not be uploaded into Azure Management portal settings, as below.
As reference, there is a blog writed by #GauravMantri, which is very helpful for you, that introduced the steps in details alought it's for Java.
Hope it helps.
I'm trying to create a very simple script that uses python's xmpppy to send a message over facebook chat.
import xmpp
FACEBOOK_ID = "username#chat.facebook.com"
PASS = "password"
SERVER = "chat.facebook.com"
jid=xmpp.protocol.JID(FACEBOOK_ID)
C=xmpp.Client(jid.getDomain(),debug=[])
if not C.connect((SERVER,5222)):
raise IOError('Can not connect to server.')
if not C.auth(jid.getNode(),PASS):
raise IOError('Can not auth with server.')
C.send(xmpp.protocol.Message("friend#chat.facebook.com","Hello world",))
This code works to send a message via gchat, however when I try with facebook I recieve this error:
An error occurred while looking up _xmpp-client._tcp.chat.facebook.com
When I remove #chat.facebook.com from the FACEBOOK_ID I get this instead:
File "gtalktest.py", line 11, in
if not C.connect((SERVER,5222)):
File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 195, in connect
if not CommonClient.connect(self,server,proxy,secure,use_srv) or secureNone and not secure: return self.connected
File "/home/john/xmpppy-0.3.1/xmpp/client.py", line 179, in connect
if not self.Process(1): return
File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 302, in dispatch
handler['func'](session,stanza)
File "/home/john/xmpppy-0.3.1/xmpp/dispatcher.py", line 214, in streamErrorHandler
raise exc((name,text))
xmpp.protocol.HostUnknown: (u'host-unknown', '')
I also notice any time I import xmpp I get the following two messages when running:
/home/john/xmpppy-0.3.1/xmpp/auth.py:24: DeprecationWarning: the sha module is deprecated; use the hashlib module instead
import sha,base64,random,dispatcher
/home/john/xmpppy-0.3.1/xmpp/auth.py:26: DeprecationWarning: the md5 module is deprecated; use hashlib instead
import md5
I'm fairly new to solving these kinds of problems, and advise, or links to resources that could help me move forward in solve these issues would be greatly appreciated. Thanks for reading!
I also started the same project, and was trapped into same problem. I found the solution too. You have to write the UserName of facebook (Hence You must opt one Username) and that too in small Caps. This is the most important part. Most probably you too like me would not be writing it in small Caps.
import xmpp
FACEBOOK_ID = "username#chat.facebook.com"
PASS = "password"
SERVER = "chat.facebook.com"
jid=xmpp.protocol.JID(FACEBOOK_ID)
client=xmpp.Client(jid.getDomain(),debug=['always'])
if not client.connect((SERVER,5222)):
raise IOError('Can not connect to server.')
if not client.auth(jid.getNode(),PASS):
raise IOError('Can not auth with server.')
message = xmpp.protocol.Message(frm=client.Bind.bound[0], to="-<#_ID_OF_FRIEND>#chat.facebook.com", typ="chat", body="Hello world",)
client.SendAndWaitForResponse(message)
This worked for me. Anyway, if you want to know the server response to your query, use Client.SendAndWaitForResponse instead of Client.send ;)