3

I am writing a Django REST API server that will return token/credentials information from Yelp. I have URL mapped this to an endpoint for testing purposes but I don't know how to fill my serializer's fields with the information returned from the call to Yelp.

When I connect to the endpoint I get the response below - there is no information... I assume this is because I need to set the values in the serializer's fields.

{
    "client_id": "",
    "client_secret": "",
    "token_path": "",
    "grant_type": "",
    "token_type": "",
    "expires_in": null,
    "access_token": ""
}

My view is...

from rest_framework import generics
from .serializers import YelpAuthenticationSerializer
from .models import YelpAuthentication

# Create your views here.
class AuthView(generics.ListCreateAPIView):
    queryset = YelpAuthentication.objects.all()
    serializer_class = YelpAuthenticationSerializer

    def perform_create(self,serializer):
        pass

And the YelpAuthentication class is

#yelp.com/developers/documentation/v3/authentication
class YelpAuthentication(models.Model):

    #fields necessary to authenticate
    url = models.CharField(max_length=200, blank=False, default='https://api.yelp.com/oauth2/token')
    api_host = models.CharField(max_length=100, blank=False, default='https://api.yelp.com')
    token_path = models.CharField(max_length=100, blank=False, default='/oauth2/token')
    grant_type = models.CharField(max_length=200, blank=False, default='client_credential')
    client_id = models.CharField(max_length=200, blank=True,default='N/A')
    client_secret = models.CharField(max_length=300, blank=True, default='N/A')

    #respone body elements after authentication
    access_token = models.CharField(max_length=200, blank=False, default='N/A')
    token_type = models.CharField(max_length=7, blank=False, default='bearer')
    expires_in = models.BigIntegerField()

    def authenticate(self):
       #some code that works
        ...
       return token

What I would like to do is...when my Serializer serializes the data, the field values have the same values as YelpAuthentication, in particular, I want the access_token to be the value returned from YelpAuthentication function.

This is how I think it should work....

    from rest_framework import serializers
    from .models import YelpAuthentication

    class YelpAuthenticationSerializer(serializers.ModelSerializer):

        class Meta:

            model = YelpAuthentication

            #I know this part is wrong...but how do I do this...
            model.authenticate()
            serializer.client_id = model.client_id
            serializer.client_secret = model.client_secret
            serializer.access_token = model.authenticate()

            #..............more manual field settings......

            fields = ('id', 'client_id','client_secret','token_path','grant_type','token_type','expires_in','access_token')
            read_only_fields = ('url', 'api_host')

My expected output is...

{
    "client_id": "N/A",
    "client_secret": "N/A",
    "token_path": "/oauth2/token",
    "grant_type": "client_credential",
    "token_type": "bearer",
    "expires_in": null,
    "access_token": "02UYO2UOUAOUF20H2NDJALNDAOFY020A80DSUFASJDFASJF03U0QUAFAHAOJSDFOUASDF0"
}
dimlee
  • 402
  • 8
  • 21

0 Answers0