Authenticate the Test User { “error_type”: “OAuthException”, “code”: 400, “error_message”: “Invalid platform app” }

后端 未结 4 1838
借酒劲吻你
借酒劲吻你 2020-12-10 00:55

I\'m trying to recover the access token via the Instagram Basic Display API but when trying to authenticate the test user I get this error:

{
    \"error_typ         


        
相关标签:
4条回答
  • 2020-12-10 01:24

    I had a similar issue and was able to resolve it by setting the content type of the request to application/x-www-form-urlencoded. below is a c# example showing how to execute the request:

    public async Task<UserTokenResponseModel> GetUserToken(string code)
        {
            var url =
                $"https://api.instagram.com/oauth/access_token";
    
            var request = new HttpRequestMessage(HttpMethod.Post, url);
    
            var client = _httpClientFactory.CreateClient();
    
            var requestContent = new List<KeyValuePair<string, string>>();
            requestContent.Add(new KeyValuePair<string, string>("client_id", ClientId));
            requestContent.Add(new KeyValuePair<string, string>("client_secret", Secret));
            requestContent.Add(new KeyValuePair<string, string>("grant_type", "authorization_code"));
            requestContent.Add(new KeyValuePair<string, string>("code", code));
            requestContent.Add(new KeyValuePair<string, string>("redirect_uri", "https://localhost:44315/instagram/authorizecallback"));
    
            request.Content = new FormUrlEncodedContent(requestContent);
    
            var response = await client.SendAsync(request);
            var content = await response.Content.ReadAsStringAsync();
    
            if (!response.IsSuccessStatusCode)
            {
                throw new Exception(content);
            }
    
            return JsonConvert.DeserializeObject<UserTokenResponseModel>(content);
        }
    
    0 讨论(0)
  • 2020-12-10 01:26

    As mentioned in other answer as well, problem was with the form body which is supposed to be send in x-www-form-urlencoded format. It was working fine for me in postman but to implement the same in angular is slightly typical. Here the post request body first has to converted in the HttpParams format and then pass on to the 'body ' parameter of post request as string like this..

    import { Injectable } from '@angular/core';
    import { HttpClient, HttpHeaders, HttpParams } from '@angular/common/http';
    
    @Injectable({
      providedIn: 'root'
    })
    export class appService {
    
      constructor(private http: HttpClient) { }
    
      public getInstaAccessToken(formData) {
        let full_url = "https://api.instagram.com/oauth/access_token";
        let body = new HttpParams()
          .set("client_id" , "YOUR_CLIENT_ID")
          .set("client_secret","YOUR_CLIENT_SECRET")
          .set("code","code received from redirect url")
          .set("grant_type","authorization_code")
          .set("redirect_uri","your redirect uri")
        const requestOptions = {
          headers: new HttpHeaders().set('Content-Type', 'application/x-www-form-urlencoded')
        }
        return this.http.post(full_url, body.toString(), requestOptions).subscribe(data=>{
          console.log(data);
          /* 
            {
              "access_token": "IGQVJ...",
              "user_id": 17841405793187218
            }
          */
        })
      }
    
    }
    
    0 讨论(0)
  • 2020-12-10 01:32

    Felice!

    When setting up an Instagram app, you should use the platform specific App ID and not the generic one setup on Facebook.

    In your Facebook app Dashboard go to Products > Instagram > Basic Display and should see the Instagram App ID.

    Use that in your authorization URL and it should work.

    0 讨论(0)
  • 2020-12-10 01:37

    Passing parameters through the body and in x-www-form-urlencoded works fine as you can see in the picture below

    0 讨论(0)
提交回复
热议问题