首页 > 解决方案 > Fitbit URL 回调给出 NULL 响应

问题描述

我无法从回调 uri 中获得响应,非常感谢您能给我的任何帮助。

我正在尝试使用 Fitbit API,它要求您使用回调 url 来获取验证码。

Workflow:
1. Go to Fitbit url to get user to allow the app access to their personal data.
2. User agrees to the conditions
3. User gets redirected to my API
4. The API returns the code from (Code is located in URL and I can access it)
5. I console.log the code out to verify it
6. API returns the code
7. I work with code then exchanging it for an access token.

问题是当我返回应用程序时,我没有返回代码(或任何东西),即使我可以在 API 上进行控制台登录。我得到的回应是NULL

这是网址:

url = "https://www.fitbit.com/oauth2/authorize?response_type=code&client_id=CLIENT_ID&redirect_uri=https://REDIRECT_URL&scope=activity%20heartrate%20location%20nutrition%20profile%20settings%20sleep%20social%20weight&expires_in=604800";
         

然后我成功打开网址InAPPBrowser

if (url !== "") {
        const canOpen = await Linking.canOpenURL(url)
        if (canOpen) {
              try {
                const isAvailable = await InAppBrowser.isAvailable()
                
                if (isAvailable) {

                  const result =InAppBrowser.open(url, {
                    // iOS Properties
                    dismissButtonStyle: 'done',
                    preferredBarTintColor: 'gray',
                    preferredControlTintColor: 'white',
                    // Android Properties
                    showTitle: true,
                    toolbarColor: '#6200EE',
                    secondaryToolbarColor: 'black',
                    enableDefaultShare: true,
                  }).then((result) => {

                    console.log("Response:",JSON.stringify(result))

                    Linking.getInitialURL().then(url => {
                      console.log("Tests: ",url)
                      this._setTracker(url as string);
                    });
                  })
                } else Linking.openURL(url)
              } catch (error) {
                console.log("Error: ",error)
              }
            
        }
      }

从这里成功打开 URL。

这是现在在 AWS 无服务器和 Lambda 上的 Typescript 中完成的 API

export const handler: APIGatewayProxyHandler = async (event, _context, callback) =>{
    let provider = event.path

    //prints code
    let x = event.queryStringParameters

    console.log("Code: ",x)
    
    

    const response = {
        statusCode: 200,
        body: "Success"
      };
      return response;
}

请让我知道是否需要进一步的细节?

谢谢!

标签: react-nativeaws-lambdareact-native-androidreact-native-iosaws-serverless

解决方案


是的,事实证明我所做的事情是正确的,除了响应应该是 301,它是一个重定向响应。

const response= {
        statusCode: 301,
        headers: {
            "location": `app://CALLBACK RESPONSE ADDRESS?type=${provider}`
        },
        body: "Boom"
    }

推荐阅读