본문으로 바로가기

[Flutter]Apple OAuth invalid_client 오류

category TIL 3개월 전

 

    final accessToken = (await requestAppleTokens(
      authCode,
      clientSecret,
      clientId,
    ))['access_token'] as String;
    const String tokenTypeHint = 'access_token';
    
 //Apple OAuth 토큰 요청
Future<Map<String, dynamic>> requestAppleTokens(
  String authorizationCode,
  String clientSecret,
  String clientId,
) async {
  final response = await http.post(
    Uri.parse('https://appleid.apple.com/auth/token'),
    headers: {
      'Content-Type': 'application/x-www-form-urlencoded',
    },
    body: {
      'client_id': clientId, 
      'client_secret': clientSecret, 
      'code': authorizationCode, 
      'grant_type': 'authorization_code', 
    },
  );

  if (response.statusCode == 200) {
    return jsonDecode(response.body);
  } else if (response.statusCode == 400 && response.body.contains('invalid_grant')) {
    // 인증 코드가 이미 사용된 경우 처리
    throw Exception('인증 코드가 이미 사용되었습니다. 새 인증 코드를 요청하세요.');
  } else {
    throw Exception('토큰 요청 실패: ${response.body}');
  }
}

 

토큰을 받아와야 하는데 

토큰 요청 실패: {error:"invalid_client"} 오류가 뜨는데

client Id, clientSecret, ... 도대체 뭐가 문제인지 알 수가 없어서 하나하나 다 체크해본 결과,

상당히 간단하게 해결되었다.

team Id 를 애플 개발자 계정 아이디로 적어준 것이 문제였다.

team Id를  app Id로 바꿔주니 바로 잘 받아오는 것을 확인.....