I need to remove user from slack team (totally), via API using Python. Recently I've found the undocumented methods https://github.com/ErikKalkoken/slackApiDoc
But users.admin.setInactive is not working correctly. It seems that there is no such method, because I get an AttributeError: 'UsersAdmin' object has no attribute 'setInactive'
Is there any way to achieve my goal?
Unfortunately this method is not available for Slack teams that use the free tier, so I can not fully test it. However, I am still getting the error "paid_only" when I call it, which would suggest that the API method itself does exist.
Based on your error it sounds more like in error in the implementation of the access layer to the API (e.g. if you are using 3rd party library it may not include this method).
You can verify by calling the method directly, which should work if you are on a paid tier:
https://slack.com/api/users.admin.setInactive?token=TOKEN&user=U12345678
If that works your issue is in the access layer of your code / the 3rd party library you are using.
Related
I'm trying to test batch enrollment for build up the google classroom, but I found some compile error at batch.execute(http=**http**)
error message is "Undefinded variable: http"
this is the code and compile error occurred the last part of this code.
def callback(request_id, response, exception):
if exception is not None:
print('Error adding user "{0}" to the course course: {1}'.format(
request_id, exception))
else:
print('User "{0}" added as a student to the course.'.format(
response.get('profile').get('name').get('fullName')))
batch = service.new_batch_http_request(callback=callback)
for student_email in student_emails:
student = {
'userId': student_email
}
request = service.courses().students().create(courseId=course_id,
body=student)
batch.add(request, request_id=student_email)
batch.execute(http=http)
and the reference is below that.
https://developers.google.com/classroom/guides/batch#python
Somebody help me.
Let's dig into the documentation to find the answer!
The error appears on a call to the execute function of the batch object. So what kind of an object is batch?
It was returned from a call to the new_batch_http_request of the service object. So what kind of an object is service?
It's not created in the reference code you cited, so we have to rummage around in the document to find some other code where something named service is created. The authors probably assumed that service in this example is the same kind of object.
The Quickstart page for Python seems like a good place to look and sure enough, there is code that creates an object named service there:
service = build('classroom', 'v1', credentials=creds)
So we can guess that service was returned from a call to build. So what is build?
It's defined in that same sample:
from googleapiclient.discovery import build
So now we have to find the documentation for googleapiclient.discovery. A search for that name leads us to:
https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.discovery-module.html
Here we see that the build function returns a Resource. So service is a Resource.
But when we look at the documentation for Resource, it has no function new_batch_http_request. What foul treachery is this?
"new_batch_http_request" looks like a pretty distinctive string, so we can search for that. Let's try the search box of the Google Classroom API site.
It only finds the Batching Requests page where we started. But it also offers to search all of Google Developers. So let's do that.
The search results then show that there are a lot of APIs with functions with this name and they all seem to create BatchHttpRequest objects, so the one for Resource probably does, too. So what is a BatchHttpRequest? Let's do another search for this name!
It turns out to be documented as part of the http module of the googleapiclient package at
https://googleapis.github.io/google-api-python-client/docs/epy/googleapiclient.http.BatchHttpRequest-class.html
The error came from a call to the execute function of the batch object, which we have guessed to be a BatchHttpRequest. Fortunately, a BatchHttpRequest has an execute function and that execute function takes an http argument. The documentation of the execute function says that the http argument can be omitted, but if it is supplied, it should be an httplib2.Http.
The reference code passes an object called http as the parameter also called http, but doesn't bother to construct it. That's why the compiler is complaining that it's undefined.
So to get the code to compile, you should be able just to omit the http=http parameter. Then maybe it will run and maybe it won't. If it doesn't, you will have to figure out how to construct an appropriate Http object using the httplib2 library, with documentation at:
https://httplib2.readthedocs.io/en/latest/libhttplib2.html#http-objects
I am new to python.
I need to get the Usage details using python sdk.
I am able to do the same using the usage detail API.
But unable to do so using the sdk.
I am trying to use the azure.mgmt.consumption.operations.UsageDetailsOperations class. The official docs for UsageDetailsOperations
https://learn.microsoft.com/en-us/python/api/azure-mgmt-consumption/azure.mgmt.consumption.operations.usage_details_operations.usagedetailsoperations?view=azure-python#list-by-billing-period
specifies four parameters to create the object
(i.e.client:Client for service requests,config:Configuration of service client,
serializer:An object model serializer,deserializer:An object model deserializer).
Out of these parameters I only have the client.
I need help understanding how to get the other three parameters or is there another way to create the UsageDetailsOperations object.
Or is there any other approach to get the usage details.
Thanks!
This class is not designed to be created manually, you need to create a consumption client, which will have an attribute "usages" which will be the class in question (instanciated correctly).
There is unfortunately no samples for consumption yet, but creating the client will be similar to creating any other client (see Network client creation for instance).
For consumption, what might help is the tests, since they give some idea of scenarios:
https://github.com/Azure/azure-sdk-for-python/blob/fd643a0/sdk/consumption/azure-mgmt-consumption/tests/test_mgmt_consumption.py
If you're new to Azure and Python, you might want to do this quickstart:
https://learn.microsoft.com/en-us/azure/python/python-sdk-azure-get-started
Feel free to open an issue in the main Python repo, asking for more documentation about this client (this will help prioritize it):
https://github.com/Azure/azure-sdk-for-python/issues
(I'm working at Microsoft in the Python SDK team).
I am looking for a way to view the header information of my requests when calling the Shopify API.
The API documentation says:
You can check how many calls you've already made using the Shopify header that was sent in response to your API call:
HTTP_X_SHOPIFY_SHOP_API_CALL_LIMIT (lists how many calls you've made for that particular shop)
On a Shopify support page I found a thread asking this same question, and Shopify's response was:
If (you are using) the python adapter then you probably need to use the get_headers method. I'm not sure, I don't use this library.
When exploring I have tried calls like the following:
shopify.ShopifyResource.get_headers().get('HTTP_X_SHOPIFY_API_CALL_LIMIT')
>>None
Was able to access it via:
resp_header = shopify.ShopifyResource.connection.response.headers["X-Shopify-Shop-Api-Call-Limit"]
However, I also saw on another thread that there is also an API Key Global limit that should be accessible somewhere through: 'x-shopify-api-call-limit'
I've worked out how OAuth2 works (via https://developers.google.com/api-client-library/python/guide/aaa_oauth) and now have an OAuth2Credentials object (let's call the object credentials) that I want to use for Google Apps provisioning purposes (the example here is using sites, but could be any of the gdata apis)
If I try:
client = gdata.sites.client.SitesClient(site="test-site",domain='my.domain')
client = credentials.authorize(client)
I get
TypeError: new_request() got an unexpected keyword argument 'http_request'
when I try to do anything
If I try
client = gdata.sites.client.SitesClient(site="test-site",domain='my.domain', auth_token=credentials)
or
client = gdata.sites.client.SitesClient(site="test-site",domain='my.domain', auth_token=credentials.access_token)
I get an AttributeError that the relevant object (credentials or credentials.access_token) has no attribute 'modify_request'
Any ideas what I can try?
I'm not entirely sure about Google's client code, but you could always try (shameless plug) sanction. It's an OAuth 2.0 client I wrote a while ago available on Github and PyPI.
The upsides:
Being a whopping 55 LOC, it's tremendously easy to grok. If something goes wrong, you won't have to ask questions here.. You should be able to just understand what's going on ;)
It has been tested with 8 different providers (including Google)
The downsides:
Obviously would require a refactor of your current code
Doesn't assume (and therefore provide) persistence implementations
Doesn't provide API implementations (you have to have a basic understanding of the OAuth 2.0-exposed API that you're dealing with)
This answer says you have to monkeypatch the OAuth2Credentials object, before passing it to the SitesClient(auth_token=credentials). It has an answer showing you how to do the monkey patching
My project has been trying to implement a credential checker using scrypt. We've tried implementing our own credentials and checker objects, but we've had a lot of trouble getting pb to use them.
Pb seems hard-coded to use MD5 hashes over the wire, which absolutely won't work in our implementation; we don't have a way to get the correct password in plaintext on the server side, since we're using scrypt, so we need a way to transmit the password to be verified in plaintext instead. We've tried using twisted.cred.credentials.UsernamePassword with our credential checker, but it doesn't seem to make it to the server. (we still get _PortalAuthChallenger instead)
The ticket at http://twistedmatrix.com/trac/ticket/4398 seems to indicate that a PBServerFactory subclass is needed in order to support custom credential checkers in pb, but so far I have been completely unable to figure out what to override in order to make it use a different ICredentials implementation. Are there any examples (or even just documentation) of how to get pb to use a different credentials class?
PB isn't exactly hard-coded to use MD5 hashes over the wire; that's just the authentication protocol as it's currently implemented. You can do pretty much whatever you want by implementing your own authentication protocol - which, in PB, just means an object that you get to call some authentication methods on.
Make your own object that implements IPBRoot, and pass it to PBServerFactory. This just means you need to implement a method called rootObject which returns the root object for a particular connection (and then declare that implementation with Zope Interface, of course).
Your IPBRoot implementation should wrap a Portal, similar to similar to _PortalRoot in Twisted's implementation.
Then, make a remote method on the object returned from rootObject suitable to your application; maybe something like remote_loginPlaintext. In this method you can authenticate users however you want, then call login on your particular Portal with whatever credentials are derived from that interaction and make sense for your requirements (and whatever interface, although for hopefully obvious reasons, IPerspective is what I'd recommend).
The fact that the somewhat inflexible _PortalRoot (which only supports 2 credential types; IAnonymous and IUsernamePassword) is registered as the adapter for Portal, making it seem a bit more official than it really is. Don't think of it is as the "official" PB/Cred integration mechanism, just the "default" one.
It would be great if you could contribute a more flexible authentication mechanism (perhaps a full SASL implementation?) for PB so that we could support other authentication types. I hope that you'll consider doing that when your application's particular needs are met.
Here's a link to the preliminary fix we came up with: http://paste.skewedaspect.com/show/20/
Note that this requires the custom credential to be Copyable, and allows the default MD5 key exchange behavior to be controlled by the keyword arg useMD5Challenge.
Note: in our implementation we're leaving the checking entirely to the Checker and having our Credential object contain nothing but username and password, so no actual code is being serialized.