Square OAuthApi:续订和撤销访问令牌

时间:2018-09-17 16:57:18

标签: api oauth square-connect

我正在尝试使用recently released OAuthApi更新和撤消Square OAuth访问令牌。我遇到的问题是from django.db import models # Create your models here. from django.contrib.auth.models import User, Group from django.db import models from django.core.mail import EmailMessage from django.contrib import admin # Create your models here. class Project(models.Model): STATUS_CHOICE = ( ('Work Assigned', 'Work Assigned'), ('Work in Progress', 'Work in Progress'), ('Testing', 'Testing'), ('Completed', 'Completed') ) project_name = models.CharField(max_length=100) project_description = models.CharField(max_length=100) status_of_the_project = models.CharField(max_length=18, choices=STATUS_CHOICE) created = models.DateTimeField(auto_now_add=True, null=True, blank=True) finish_date = models.DateTimeField(null=True, blank=True) supporting_documents = models.FileField(null=True, blank=True) admin = models.ForeignKey(Person, on_delete=models.CASCADE) class FlatPageAdmin(admin.ModelAdmin): fieldsets = ( (None, { 'fields': ('project_name','project_description','status_of_the_project','created','finish_date','supporting_documents',) }) ) def __str__(self): return self.Project_Name class Meta: verbose_name = "Project" verbose_name_plural = "Projects" class Person(models.Model): PERSON_TYPE = ( ('Admin', 'Admin'), ('Project Manager', 'Project Manager'), ('Technician', 'Technician'), ('Tester', 'Tester') ) user = models.OneToOneField(User, on_delete=models.CASCADE, related_name='user_person') projects = models.ManyToManyField(Project, null=True, related_name='people') mail_id = models.EmailField(max_length=50, blank=True, null=True) person_type = models.CharField(max_length=18, choices=PERSON_TYPE) class Meta: verbose_name = "Person" verbose_name_plural = "People" class Bug(models.Model): STATUS_CHOICE = ( ('Unassigned', 'Unassigned'), ('Assigned', 'Assigned'), ('Testing', 'Testing'), ('Tested', 'tested'), ('Fixed', 'Fixed') ) SITUATION_TYPE = ( ('Bug', 'Bug'), ('Issue', 'Issue'), ('Enhancement', 'Enhancement'), ('Not an issue or bug', 'Not an issue or bug'), ('Fixed', 'Fixed') ) project = models.ForeignKey(Project, on_delete=models.CASCADE) issue_title = models.CharField(max_length=50, blank=True, null=True) situation_type = models.CharField(max_length=25, choices=SITUATION_TYPE) basic_description = models.CharField(max_length=100) detailed_description = models.TextField(default='The Description, here.') status = models.CharField(max_length=18, choices=STATUS_CHOICE) assigned_to = models.ForeignKey(Person, on_delete=models.CASCADE) # assigned_to_mail_ID - this can be pulled from the assigned_to relationship # Admin name and ID can be pulled from the project->people relationship reported_by = models.CharField(max_length=50, blank=True, null=True) reporters_mail_id = models.EmailField(max_length=50, blank=True, null=True) reported_date = models.DateTimeField(null=True, blank=True) created = models.DateTimeField(auto_now_add=True, null=True, blank=True) updated = models.DateTimeField(auto_now=True, null=True, blank=True) deadline_date = models.DateTimeField(null=True, blank=True) supporting_documents_by_reporter = models.FileField(null=True, blank=True) project_managers_comment = models.TextField(default='The Description, here.') supporting_documents_by_project_manager = models.FileField(null=True, blank=True) technicians_comment = models.TextField(default='The Description, here.') supporting_documents_by_technician = models.FileField(null=True, blank=True) testers_comment = models.TextField(default='The Description, here.') supporting_documents_by_tester = models.FileField(null=True, blank=True) def __str__(self): return '{} ({}) [{} {}]'.format(self.project, self.situation_type, self.status, self.issue_title) def save(self, force_insert=False, force_update=False, using=None, update_fields=None): if self.id: user=self.assigned_to.user self.assigned_to.mail_id=user.email send_mail(self.project.admin.mail_id, ass=self.assigned_to.mail_id) super(Bug, self).save() class Meta: verbose_name = "Project Task/Issue" verbose_name_plural = "Project Tasks/Issues" def send_mail(admin, ass): email=EmailMessage('Changes made to Task','Changes have been made to one of your Task reports and we hereby request you to have a look at it at the earliest.', to=[admin, ass]) email.send()

看着renewTokenWithHttpInfo()revokeTokenWithHttpInfo()方法,我看不到[HTTP/1.1 400 Bad Request] ... "message": "missing authorization header"头设置在什么地方,也没有使用时传递应用程序客户端密码的明显方法。 renewToken()revokeToken()

我想念什么?

2 个答案:

答案 0 :(得分:0)

Square Dev Slack Channel中的Python回答了这个问题。

在调用API之前,您需要同时设置应用程序密码和标头前缀。

导入必要的SDK元素:

from squareconnect.apis.o_auth_api import OAuthApi
from squareconnect.models.renew_token_request import RenewTokenRequest

创建您的OAuth API实例:

oauth_api = OAuthApi()

设置以下两个变量:

oauth_api.api_client.configuration.api_key['Authorization'] = <Your Application Secret>
oauth_api.api_client.configuration.api_key_prefix['Authorization'] = 'Client'

RenewTokenRequest的新实例:

renew_token_request_body = RenewTokenRequest()

向其提供您要续订的访问令牌:

 renew_token_request_body.access_token = <The Access Token to Renew>

然后进行API调用(您的Square App ID也称为客户ID):

response = oauth_api.renew_token(<Your Square App ID>, renew_token_request_body)

答案 1 :(得分:0)

我在最近的一个项目中遇到了同样的问题。这样的事情将设置默认配置并使用PHP SDK撤销令牌:

function revokeToken( $oauthToken ) {

    $application_id = [YOUR APP ID];
    $application_secret = [YOUR OAUTH SECRET];
    $local = [TRUE or FALSE];

    // set up default authorization
    \SquareConnect\Configuration::getDefaultConfiguration()->setApiKey( 'Authorization', $application_secret );

    // setting 'Client' here vs. the default 'Bearer' revoked the token correctly
    \SquareConnect\Configuration::getDefaultConfiguration()->setApiKeyPrefix( 'Authorization', 'Client' );

    // if you're testing on localhost, disable SSL checks using your own code
    // for production make sure $local is FALSE
    if ( $local ) {
        \SquareConnect\Configuration::getDefaultConfiguration()->setSSLVerification( FALSE );
    }

    // Create an OAuth API client
    $oauthApi = new \SquareConnect\Api\OAuthApi(); 
    $body = new \SquareConnect\Model\RevokeTokenRequest();

    // Set the POST body
    $body->setClientId( $application_id ); // main application ID
    $body->setAccessToken( $oauthToken ); // the OAUTH token of the merchant to remove

    try {
        $result = $oauthApi->revokeToken( $body );
    } catch ( Exception $e ) {
        Log::Error( 'Exception when calling OAuthApi->revokeToken: ' . $e->getMessage() );
        throw new Exception( "Error Processing Request: Token revocation failed!", 1 );
    }

    return;
}

}

有关如何包括SDK和配置文件(如果需要)的更多信息,请参见https://developer.squareup.com/docs/oauth-api/cookbook/revoke-oauth-tokens